FAQ
Here's the short version to get something up-and-running:

// in the server
http.Handle("/listen", websocket.Handler(func(conn *websocket.Conn){
defer conn.Close()

err := websocket.Message.Send(conn, "HELLO")
if err != nil {
// note on graceful exit err == io.EOF
log.Println("WRITE ERROR:", conn, err)
return
}

// alternatively have two goroutines one that reads and the other that sends
for {
var message string
err = websocket.Message.Receive(conn, &message)
if err != nil {
log.Println("READ ERROR:", conn, err)
break
}

err = websocket.Message.Send(conn, message)
if err != nil {
log.Println("WRITE ERROR:", conn, err)
break
}
}
}))

// in the client
var conn = new Websocket("ws://" + window.location.host + "/listen");
conn.onmessage = function(ev){
console.log("received", ev.data);
};
conn.onopen = function(ev){ console.log("the comms are open", ev); };
conn.onclose = function(ev){ console.log("the comms are closed", ev); };
conn.onerror = function(ev){ console.log("the comms are malfunctioning",
ev); };

// before trying to send you need to wait before the connection is open:
conn.send("PING");
conn.send(JSON.serialize({"hello": "world"}));

You can take a look at a basic websocket setup
here: https://github.com/loov/spaceshift *(sorry, code a bit messy, wrote
it in ~5h and I haven't had a chance to clean it up).*

Also you can avoid JS (to some degree) if you use GopherJS :)
https://github.com/gopherjs/websocket/tree/master/test

+ Egon
On Wednesday, 27 April 2016 07:00:24 UTC+3, kortschak wrote:

I'm have close to zero experience writing things that interact with
users via a web browser, but now I am in the position of wanting to make
something like this.

Can someone point me to good resources that I can dive into to learn how
to do this?

There are two goals of the project:

1. Take user input in the form of mouse clicks and text field entries
sending events to the server without updating the page.

2. Pushing data from the server to the page in real time.

(The back end is or will be written in Go).

These both seem trivially feasible on the basis of what I interact with
as a user of a browser, but I have zero clue how to achieve either of
them other an it will involve javascript.

Pointers gratefully accepted.

Dan
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 10 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 27, '16 at 4:00a
activeMay 2, '16 at 11:18a
posts10
users7
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase