go websockets eof

huangapple go评论88阅读模式
英文:

go websockets eof

问题

我正在尝试制作一个简单的命令转发器,将我的家用电脑连接到我拥有的服务器,这样我就可以将命令推送到我的服务器,然后我的家用电脑就可以接收到。这些命令是用于控制我的下载器的简单暂停/恢复命令。我的设计是,在服务器上运行一个中心实例,它创建一个窗口用于传递命令,并创建一个窗口用于将这些命令传递给我的电脑。我用一个通道将这两个“窗口”绑定在一起,它们运行一个服务器。当客户端连接并向中心发送消息时,消息通过通道流式传输到后端窗口,然后传递给真正的后端(在我的家用电脑上)。当后端响应中心窗口上的后端时,中心将结果打印回客户端。

使用这种方法,只有第一条消息能够传递并与我的下载器一起工作。每次收到消息时,我都必须重新连接来自我的家用电脑的后端与中心,以使其正常工作。我不认为这是使用WebSockets的正确方式,所以我来这里寻求帮助。在成功的请求之后(当后端完成工作并回复结果时),它会一直循环并出现EOF错误。

代码的重要部分包括:

如果将源代码放在GOPATH中(我正在为最新版本的Go开发它以支持现代WebSockets),编译它:
go build gosab/cmd,运行它:

  • ./cmd -mode="hub" 中心
  • ./cmd -mode="backend" --address="localhost:8082" 后端

要将消息传递给中心,请使用以下JavaScript代码:

var s = new WebSocket("ws://localhost:8082")
s.send("1 5")

那么我该如何处理它?通道是在两个不同请求之间进行通信的好方法吗?

英文:

I'm trying to make a simple command forwarder to connect my home computer to a server I own, so that I can push commands to my server and my home pc gets it. Those commands are simple pause/resume for my downloader. My design is, that on a server, I run a hub instance, which creates a window for passing commands and a window for backend to pass those commands to my pc. I'm bounding those two "windows" with a channel, they run a server. When a client connects and sends a message to the hub, it gets streamed through a channel to backend window and then to the real backend (on my home pc). When backend responds to the backend window on the hub, the hub prints the result back to the client.

With this approach, only the first message passes and works with my downloader. I have to reconnect the backend from my home pc with the hub each time I get a message to get this working properly. I don't think that's the proper way with websockets, so here I am. After one successful request (when the backend finishes it's work and replies the result), it gets looped forever with EOF error.

The important parts of the code are:

If you put the source in your GOPATH (i'm developing it for the tip version of go to support modern websockets), to compile it:
go build gosab/cmd, to run it:

  • ./cmd -mode="hub" hub
  • ./cmd -mode="backend" --address="localhost:8082" backend

To pass messages to the hub, use this javascript:

var s = new WebSocket("ws://localhost:8082")
s.send("1 5")

So how do I handle it? Are channels a good way to communicate between two different requests?

答案1

得分: 7

我很惊讶你还没有收到关于这个问题的答案。

你需要做的是像下面的代码一样。当你接收到一个传入的websocket连接时,会为该连接生成一个新的goroutine。如果让这个goroutine结束,它将断开websocket客户端的连接。

我假设你不一定会在同一台计算机上运行客户端和服务器。如果你总是这样做,那么最好通过通道或其他方式在内部进行通信,而不是使用websocket或网络端口。我之所以提到这一点,是因为我不完全确定你在使用这个做什么。我只希望我回答了你问题的正确部分。

package main

import (
	"code.google.com/p/go.net/websocket"
	"flag"
	"fmt"
	"net/http"
	"os"
	"time"
)

type Message struct {
	RequestID      int
	Command        string
	SomeOtherThing string
	Success        bool
}

var mode *string = flag.String("mode", "<nil>", "Mode: server or client")
var address *string = flag.String("address", "localhost:8080", "Bind address:port")

func main() {
	flag.Parse()

	switch *mode {
	case "server":
		RunServer()
	case "client":
		RunClient()
	default:
		flag.Usage()
	}
}

func RunServer() {
	http.Handle("/", http.FileServer(http.Dir("www")))
	http.Handle("/server", websocket.Handler(WSHandler))
	fmt.Println("Starting Server")
	err := http.ListenAndServe(*address, nil)
	if err != nil {
		fmt.Printf("HTTP failed: %s\n", err.Error())
		os.Exit(1)
	}
}

func WSHandler(ws *websocket.Conn) {
	defer ws.Close()
	fmt.Println("Client Connected")
	for {
		var message Message
		err := websocket.JSON.Receive(ws, &message)
		if err != nil {
			fmt.Printf("Error: %s\n", err.Error())
			return
		}
		fmt.Println(message)

		// do something useful here...

		response := new(Message)
		response.RequestID = message.RequestID
		response.Success = true
		response.SomeOtherThing = "The hot dog left the castle as requested."
		err = websocket.JSON.Send(ws, response)
		if err != nil {
			fmt.Printf("Send failed: %s\n", err.Error())
			os.Exit(1)
		}
	}
}

func RunClient() {
	fmt.Println("Starting Client")
	ws, err := websocket.Dial(fmt.Sprintf("ws://%s/server", *address), "", fmt.Sprintf("http://%s/", *address))
	if err != nil {
		fmt.Printf("Dial failed: %s\n", err.Error())
		os.Exit(1)
	}
	incomingMessages := make(chan Message)
	go readClientMessages(ws, incomingMessages)
	i := 0
	for {
		select {
		case <-time.After(time.Duration(2e9)):
			i++
			response := new(Message)
			response.RequestID = i
			response.Command = "Eject the hot dog."
			err = websocket.JSON.Send(ws, response)
			if err != nil {
				fmt.Printf("Send failed: %s\n", err.Error())
				os.Exit(1)
			}
		case message := <-incomingMessages:
			fmt.Println(message)
		}
	}
}

func readClientMessages(ws *websocket.Conn, incomingMessages chan Message) {
	for {
		var message Message
		err := websocket.JSON.Receive(ws, &message)
		if err != nil {
			fmt.Printf("Error: %s\n", err.Error())
			return
		}
		incomingMessages <- message
	}
}
英文:

I'm surprised you haven't received an answer to this.

What you need to do is something like the code below. When you receive an incoming websocket connection, a new goroutine is spawned for that connection. If you let that goroutine end, it'll disconnect the websocket client.

I'm making an assumption that you're not necessarily going to be running the client and server on the same computer. If you always are, then it'd be better to do the communication internally via channels or such instead of using websockets or a network port. I only mention this because I'm not completely sure what you're using this for. I just hope I answered the right part of your question.

package main
import (
&quot;code.google.com/p/go.net/websocket&quot;
&quot;flag&quot;
&quot;fmt&quot;
&quot;net/http&quot;
&quot;os&quot;
&quot;time&quot;
)
type Message struct {
RequestID      int
Command        string
SomeOtherThing string
Success        bool
}
var mode *string = flag.String(&quot;mode&quot;, &quot;&lt;nil&gt;&quot;, &quot;Mode: server or client&quot;)
var address *string = flag.String(&quot;address&quot;, &quot;localhost:8080&quot;, &quot;Bind address:port&quot;)
func main() {
flag.Parse()
switch *mode {
case &quot;server&quot;:
RunServer()
case &quot;client&quot;:
RunClient()
default:
flag.Usage()
}
}
func RunServer() {
http.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;www&quot;)))
http.Handle(&quot;/server&quot;, websocket.Handler(WSHandler))
fmt.Println(&quot;Starting Server&quot;)
err := http.ListenAndServe(*address, nil)
if err != nil {
fmt.Printf(&quot;HTTP failed: %s\n&quot;, err.Error())
os.Exit(1)
}
}
func WSHandler(ws *websocket.Conn) {
defer ws.Close()
fmt.Println(&quot;Client Connected&quot;)
for {
var message Message
err := websocket.JSON.Receive(ws, &amp;message)
if err != nil {
fmt.Printf(&quot;Error: %s\n&quot;, err.Error())
return
}
fmt.Println(message)
// do something useful here...
response := new(Message)
response.RequestID = message.RequestID
response.Success = true
response.SomeOtherThing = &quot;The hot dog left the castle as requested.&quot;
err = websocket.JSON.Send(ws, response)
if err != nil {
fmt.Printf(&quot;Send failed: %s\n&quot;, err.Error())
os.Exit(1)
}
}
}
func RunClient() {
fmt.Println(&quot;Starting Client&quot;)
ws, err := websocket.Dial(fmt.Sprintf(&quot;ws://%s/server&quot;, *address), &quot;&quot;, fmt.Sprintf(&quot;http://%s/&quot;, *address))
if err != nil {
fmt.Printf(&quot;Dial failed: %s\n&quot;, err.Error())
os.Exit(1)
}
incomingMessages := make(chan Message)
go readClientMessages(ws, incomingMessages)
i := 0
for {
select {
case &lt;-time.After(time.Duration(2e9)):
i++
response := new(Message)
response.RequestID = i
response.Command = &quot;Eject the hot dog.&quot;
err = websocket.JSON.Send(ws, response)
if err != nil {
fmt.Printf(&quot;Send failed: %s\n&quot;, err.Error())
os.Exit(1)
}
case message := &lt;-incomingMessages:
fmt.Println(message)
}
}
}
func readClientMessages(ws *websocket.Conn, incomingMessages chan Message) {
for {
var message Message
err := websocket.JSON.Receive(ws, &amp;message)
if err != nil {
fmt.Printf(&quot;Error: %s\n&quot;, err.Error())
return
}
incomingMessages &lt;- message
}
}

huangapple
  • 本文由 发表于 2012年2月17日 03:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/9317392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定