Google Web Sockets

I got all excited yesterday about the inclusion of Web Sockets in Google Chrome, and even more excited today when I noticed the latest version of Google Go now has a server side implementation of Web Sockets. Naturally I had to write a quick test to check it out :-) Here's the Google Go server implementation:

package main
import ( "http"; "io"; "websocket"; )
func EchoServer(ws *websocket.Conn) {
  io.Copy(ws, ws);
}
func main() {
  http.Handle("/echo", websocket.Handler(EchoServer));
  err := http.ListenAndServe(":12345", nil);
}

And here's the client side javascript that updates the contents of a DIV named 'banner'...

if ("WebSocket" in window) {
  var ws = new WebSocket("ws://localhost:12345/echo");
  ws.onopen = function(){ws.send("My first WebSocket worked!");};
  ws.onmessage = function(evt){document.getElementById('banner').innerText=evt.data;};
  ws.onclose = function(){};
}