Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving a websocket in Go

Tags:

websocket

go

I'm starting to play around with websockets + go and well I think I'm misunderstanding something quite basic with websockets in Go.

I'd like to simply listen for a websocket connection and process accordingly. However all examples I see in Go using websocket is serving the web page that then connects to the websocket, is this a requirement?

The following is a basic echo server I have setup:

package main

import (
  "fmt"
  "code.google.com/p/go.net/websocket"
  "net/http"
)

func webHandler(ws *websocket.Conn) {
  var s string
  fmt.Fscan(ws, &s)
  fmt.Println("Received: ", s)
}

func main() {
  fmt.Println("Starting websock server: ")
  http.Handle("/echo", websocket.Handler(webHandler))
  err := http.ListenAndServe(":8080", nil)
  if err != nil {
    panic("ListenAndServe: " + err.Error())
  }
}

This is the javascript used to connect:

ws = new WebSocket("ws://localhost:8080/echo");
ws.onmessage = function(e) {
    console.log("websock: " + e.data);
};

However this results in: WebSocket connection to 'ws://localhost:8080/echo' failed: Unexpected response code: 403

like image 623
Gybe Avatar asked Oct 31 '13 13:10

Gybe


1 Answers

When working with websockets from Javascript, you will seldom have to read the frames directly. To be honest, I am not even sure how to do that.

Fortunately, the websocket package already has a type, Codec that does this for you. My suggestion is to use the predefined websocket.Message codec to Recieve and Send messages instead.

Message is a codec to send/receive text/binary data in a frame on WebSocket connection. To send/receive text frame, use string type. To send/receive binary frame, use []byte type.

Using websocket.Message, your webHandler would look something like this:

func webHandler(ws *websocket.Conn) {
    var in []byte
    if err := websocket.Message.Receive(ws, &in); err != nil {
        return
    }
    fmt.Printf("Received: %s\n", string(in))
    websocket.Message.Send(ws, in)      
}

And, no, it is not a requirement that Go serves the webpage. The 403 error you received does not have to do with Go or the websocket package.

like image 129
ANisus Avatar answered Sep 28 '22 08:09

ANisus