FAQ
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>

--
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

  • Reed O'Brien at Feb 23, 2016 at 8:56 pm
    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


    On Tuesday, February 23, 2016 at 12:27:16 PM UTC-8, anthon...@gmail.com
    wrote:
    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>
    --
    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.
  • C Banning at Feb 24, 2016 at 2:03 pm
    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.)


    On Tuesday, February 23, 2016 at 2:27:16 PM UTC-6, anthon...@gmail.com
    wrote:
    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>
    --
    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.
  • Anthonykwest0 at Feb 26, 2016 at 6:50 am
    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

    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 23, '16 at 8:26p
activeFeb 26, '16 at 6:50a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase