Golang Server close the connection of the client : websocket

1,088 views
Skip to first unread message

farah

unread,
May 7, 2014, 9:15:47 AM5/7/14
to golan...@googlegroups.com
Hello!  i have a problem with my golang server in which i'm using websockets.
The server opens the connection and the client could connect to it, but the problem is that when the server starts sending the data to the client, the client connection is closed after a small period of time. i suggest that the problem is with the server and not with the client because i tried to connect to the server with another web client, and it's the same issue. I didn't understand the cause ! Can someone help me?
this is server.go:


/********************************server.go***********************************/

func Echo(ws *websocket.Conn) {
fmt.Println("Echoing")

         for {
   msg := MessageReceived{Name: "OrderCommand", Nbmsg: 3}

        if err := websocket.JSON.Send(ws, msg); err != nil {
            fmt.Println("Can't send")
            break
        }

//os.Exit(0)
}
}

func checkError(err error) {
if err != nil {
Log("Fatal error ", err.Error())
os.Exit(1)
}
}


func main {

    http.HandleFunc("/save", saveHandler)
    http.Handle("/", websocket.Handler(Echo))
err:= http.ListenAndServe(":8081", nil)
checkError(err)

}




/********************************client.go***********************************/
/* EchoClient
 */
package main

import (
"fmt"
"log"
)

func main() {
origin := "http://localhost/"
url := "ws://localhost:8081/echo"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}

var msg = make([]byte, 512)
var n int
if n, err = ws.Read(msg); err != nil {
log.Fatal(err)
}
fmt.Printf("Received: %s.\n", msg[:n])
}




 

Andy Bonventre

unread,
May 7, 2014, 1:46:30 PM5/7/14
to farah, golan...@googlegroups.com
The server does, in fact, receive the message just fine. The issue is that the client only sends one message and then disconnects due to the program exiting, so the next iteration of the loop causes "can't send" to be printed. It’s trying to send on a broken pipe.

Here is a working example of the client and server based on your code – https://gist.github.com/andybons/a4bccdb21417989a60ad

A


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Farah Jaziri

unread,
May 7, 2014, 4:10:37 PM5/7/14
to Andy Bonventre, golan...@googlegroups.com
Thank you Andy!  I'm new with go so i didn't understand well why the client deconnects when it sends the message and as i can see, the problem still , the client is still deconnecting after sending the message also after i added the loop for! Thank you

Ross Salas

unread,
May 7, 2014, 4:48:00 PM5/7/14
to Farah Jaziri, Andy Bonventre, golan...@googlegroups.com
I added a for loop in the client and it doesn't disconnect.  My client main() looks like this:


func main() {
        origin := "http://localhost/"
        url := "ws://localhost:8081/echo"
        ws, err := websocket.Dial(url, "", origin)
        if err != nil {
                log.Fatal(err)
        }

        var msg = make([]byte, 512)
        var n int
        for {

                if n, err = ws.Read(msg); err != nil {
                        log.Fatal(err)
                }
                fmt.Printf("Received: %s.\n", msg[:n])
        }
}

# c.go is the client code
[res@sjcpubdefrac1 src]# go build c.go
[res@sjcpubdefrac1 src]# ./c
Echoing
Received: {"Name":"OrderCommand","Nbmsg":3}.
Received: {"Name":"OrderCommand","Nbmsg":3}.
Received: {"Name":"OrderCommand","Nbmsg":3}.
Received: {"Name":"OrderCommand","Nbmsg":3}.
...
Reply all
Reply to author
Forward
0 new messages