Golang服务器关闭客户端连接:websocket [英] Golang Server close the connection of the client : websocket

查看:1039
本文介绍了Golang服务器关闭客户端连接:websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用websockets的golang服务器上遇到问题。
服务器打开连接,客户端可以连接到它,但问题是,当服务器开始向客户端发送数据时,客户端连接会在一段时间后关闭。我建议问题出在服务器上,而不是用客户端,因为我试图用另一个Web客户端连接到服务器,这是同样的问题。我不明白原因!有人可以帮我吗?

server.go:

$ $ p $ func Echo (ws * websocket.Conn){
fmt.Println( 回响)

为{
MSG:= {的messageReceived名称: OrderCommand,Nbmsg:3}

如果err:= websocket.JSON.Send(ws,msg); err!= nil {
fmt.Println(Can not send)
break
}

//os.Exit(0)
}
}

func checkError(err error){
if err!= nil {
Log(致命错误,err.Error())
os.Exit(1)
}
}


func main(){

http.HandleFunc(/ save ,saveHandler)
http.Handle( /,websocket.Handler(回声))
ERR:= http.ListenAndServe( :8081,无)
checkError(ERR)

}

和client.go:

 导入(
code.google.com/p/go.net/websocket
fmt
日志


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。阅读(MSG); err!= nil {
log.Fatal(err)
}
fmt.Printf(Received:%s.\\\
,msg [:n])
}


解决方案

正如其他人指出的那样,您必须收到一条消息。



目前,当有人连接时,您的程序将进入for-loop并开始用消息轰击客户端。可能不是echo服务器的预期行为。

首先您要接收消息,然后发送回复:

  func Echo(ws * websocket.Conn){
fmt.Println( 回响)

MSG:=新的(的messageReceived)

为{
//服务器块在这里,直到从所述消息客户机接收到
websocket.JSON.Receive(WS,&安培; MSG)

fmt.Printf( 接收消息:%+ v\\\
,MSG)

//重新编码相同的消息并将其发回
,如果err:= websocket.JSON.Send(ws,msg); err!= nil {
fmt.Println(Can not send echo)
break
}
}
}

一个完整的工作版本可以在Playground找到: http://play.golang.org/p/nQ3fJ5Nb0I



因为它使用websockets,所以您必须在本地计算机上编译它。


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])
}

解决方案

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.

这篇关于Golang服务器关闭客户端连接:websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆