2

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?

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)

}

and client.go:

 import (
    "code.google.com/p/go.net/websocket"
    "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])
}
3
  • Your client just reads 1 message and exits. If you put it in a loop it would work just fine. Commented May 7, 2014 at 14:35
  • i tried it: for { var msg = make([]byte, 512) var n int if n, err = ws.Read(msg); err != nil { log.Fatal(err) break } fmt.Printf("Received: %s.\n", msg[:n]) } but it exists with an EOF error Commented May 7, 2014 at 14:55
  • Pleas post a runnable example of what you're using. What I can run of the code you posted works exactly as expected. Commented May 7, 2014 at 15:06

2 Answers 2

3

Your problem, as others have pointed out, is that you must receive a message as well.

Currently, when someone connects, your program will step into the for-loop and start bombarding the client with messages. Probably not the intended behaviour of an echo server.

First you want to Receive a message, then Send a reply:

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

    msg := new(MessageReceived)

    for {
        // The server blocks here until a message from the client is received
        websocket.JSON.Receive(ws, &msg)

        fmt.Printf("Received message: %+v\n", msg)

        // Reencode the same message and send it back
        if err := websocket.JSON.Send(ws, msg); err != nil {
            fmt.Println("Can't send echo")
            break
        }
    }
}

A full working version can be found at Playground: http://play.golang.org/p/nQ3fJ5Nb0I

Since it uses websockets, you must compile it on your local computer.

Sign up to request clarification or add additional context in comments.

Comments

2

Why using ws.Read when you can use websocket.JSON.Receive to deserialize the message?

Here are the server: http://play.golang.org/p/NZ6VJ4daGm and the client: http://play.golang.org/p/rkJVKGhrGk (that I have changed to receive 10 messages before exiting).

The string "Can't send" will be printed by the server once the client closes the websocket connection.

2 Comments

i replace it but it's the same problem with me. client side: received: {orderCommand 3} exit Status 1
Exit status of client is 0 (i.e. success). The server keeps running after the client exists, and it prints "Can't send..." because the client has closed the connection. It works as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.