Websocket not working

464 views
Skip to first unread message

anthon...@gmail.com

unread,
Feb 23, 2016, 3:27:16 PM2/23/16
to golang-nuts
Hi.  Golang newbie here so my apologies if this is obvious :(

I've copied and pasted a websockets example but it is not working. 

When I open the web page I get: Firefox can't establish a connection to the server at ws://localhost:8080/echo.

The code I copied is listed below.

Any help would be much appreciated.

Go code
======

package main
 
import (
    "golang.org/x/net/websocket"
    "net/http"
    "fmt"
)
 
func echoHandler(ws *websocket.Conn) {
 
  for {
    receivedtext := make([]byte, 100)
 
    n,err := ws.Read(receivedtext)
 
    if err != nil {
      fmt.Printf("Received: %d bytes\n",n)
    }
 
    s := string(receivedtext[:n])
    fmt.Printf("Received: %d bytes: %s\n",n,s)
  }
}
 
func main() {
  http.Handle("/echo", websocket.Handler(echoHandler))
  http.Handle("/", http.FileServer(http.Dir(".")))
  err := http.ListenAndServe(":8080", nil)
  if err != nil {
    panic("Error: " + err.Error())
  }
}


Web page
=======

<html>
<head>
<meta charset="UTF-8" />
<script>
 
  var serversocket = new WebSocket("ws://localhost:8080/echo");
 
  serversocket.onopen = function() {
    serversocket.send("Connection init");
  }
 
  // Write message on receive
  serversocket.onmessage = function(e) {
    document.getElementById('comms').innerHTML += "Received: " + e.data + "<br>";
  };
 
  function senddata() {
     var data = document.getElementById('sendtext').value;
     serversocket.send(data);
     document.getElementById('comms').innerHTML += "Sent: " + data + "<br>";
  }
 
</script>
 
</head>
<body>
  <input id="sendtext" type="text" />
  <input type="button" id="sendBtn" value="send" onclick="senddata()"></input>
 
  <div id='comms'></div>
 
</body>
</html>

Reed O'Brien

unread,
Feb 23, 2016, 3:56:35 PM2/23/16
to golang-nuts, anthon...@gmail.com
Are you accessing the html page via a file:///home/you/index.html?

If so that could be your issue.  Try accessing it through another web server[1] or serving it from another handler in your app.

[1] There's dozens out there, and it is as simple as https://github.com/golang/go/wiki/HttpStaticFiles, but I use the following for ad hoc local needs: https://github.com/reedobrien/dserve

C Banning

unread,
Feb 24, 2016, 9:03:42 AM2/24/16
to golang-nuts, anthon...@gmail.com
You've got a couple of things going on - 
  1. your echoHandler() needs a ws.Write(receivedtext[:n]) to "echo" the message back to the browser app.
  2. you must launch the browser app from the directory listing: http://localhost:8080 - if you launch the browser app directly from the file system the browser will block access to localhost:8080 because it looks like cross-origin resource sharing (CORS) and the web socket.Handler doesn't support that.  (That's why you get the 403 error when Firefox tries to establish the connection.)

anthon...@gmail.com

unread,
Feb 26, 2016, 1:50:24 AM2/26/16
to golang-nuts, anthon...@gmail.com
Doh, I was launching the browser app from a file.

It's working properly now.

Thanks Reed O'Brien and C Banning for your help :)

Anthony
Reply all
Reply to author
Forward
0 new messages