英文:
Golang Server close the connection of the client : websocket
问题
我有一个关于我的 Golang 服务器的问题,我在其中使用了 WebSockets。
服务器打开连接,客户端可以连接到它,但问题是当服务器开始向客户端发送数据时,客户端连接在很短的时间后就关闭了。我认为问题出在服务器上而不是客户端上,因为我尝试使用另一个 Web 客户端连接到服务器,出现了相同的问题。我不明白原因!有人可以帮助我吗?
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:
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])
}
希望对你有帮助!
英文:
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])
}
答案1
得分: 3
你的问题,正如其他人指出的,是你必须接收一条消息。
目前,当有人连接时,你的程序会进入for循环,并开始向客户端发送消息。这可能不是一个回显服务器的预期行为。
首先,你需要“接收”一条消息,然后“发送”一个回复:
func Echo(ws *websocket.Conn) {
fmt.Println("Echoing")
msg := new(MessageReceived)
for {
// 服务器在此处阻塞,直到接收到来自客户端的消息
websocket.JSON.Receive(ws, &msg)
fmt.Printf("Received message: %+v\n", msg)
// 重新编码相同的消息并发送回去
if err := websocket.JSON.Send(ws, msg); err != nil {
fmt.Println("Can't send echo")
break
}
}
}
可以在Playground上找到一个完整的工作版本:http://play.golang.org/p/nQ3fJ5Nb0I
由于它使用了WebSockets,你必须在本地计算机上编译它。
英文:
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.
答案2
得分: 2
为什么要使用ws.Read
,而不是使用websocket.JSON.Receive
来反序列化消息?
这是服务器端的代码:http://play.golang.org/p/NZ6VJ4daGm
这是客户端的代码:http://play.golang.org/p/rkJVKGhrGk(我已经修改为在退出之前接收10条消息)。
当客户端关闭websocket连接时,服务器会打印字符串"Can't send"。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论