Golang服务器关闭客户端连接:websocket

编程入门 行业动态 更新时间:2024-10-18 16:52:53
本文介绍了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/p/go/websocketfmt日志) 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找到: play.golang/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/p/go/websocket" "fmt" "log" ) func main() { origin := "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: play.golang/p/nQ3fJ5Nb0I

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

更多推荐

Golang服务器关闭客户端连接:websocket

本文发布于:2023-11-25 22:58:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   服务器   Golang   websocket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!