去 websocket eof
我正在尝试制作一个简单的命令转发器,将我的家用计算机连接到我拥有的服务器,以便我可以将命令推送到我的服务器,而我的家用电脑可以获取它。这些命令对于我的下载器来说是简单的暂停/恢复。我的设计是,在服务器上,我运行一个集线器实例,它创建一个用于传递命令的窗口和一个用于后端将这些命令传递到我的电脑的窗口。我用一个通道限制这两个“窗口”,它们运行一个服务器。当客户端连接并向集线器发送消息时,它会通过通道传输到后端窗口,然后传输到真正的后端(在我的家用电脑上)。当后端响应集线器上的后端窗口时,集线器将结果打印回客户端。
通过这种方法,只有第一条消息可以通过并与我的下载器一起使用。每次收到消息才能正常工作时,我都必须将家用电脑的后端重新连接到集线器。我认为这不是使用 websocket 的正确方法,所以我在这里。一次成功请求后(当后端完成工作并回复结果时),它会永远循环并出现 EOF 错误。
代码的重要部分是:
如果您将源代码放入 GOPATH 中(i正在为 go 的提示版本开发它以支持现代 websockets),编译它: go build gosab/cmd
,运行它:
./cmd -mode="hub"
hub./cmd -mode= "backend" --address="localhost:8082"
backend
要将消息传递到集线器,请使用以下 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很惊讶你还没有收到这个问题的答复。
您需要做的是类似于下面的代码。当您收到传入的 websocket 连接时,会为该连接生成一个新的 goroutine。如果你让该 goroutine 结束,它会断开 websocket 客户端的连接。
我假设您不一定要在同一台计算机上运行客户端和服务器。如果您总是这样,那么最好通过通道等进行内部通信,而不是使用 websockets 或网络端口。我只是提到这一点,因为我不完全确定你用它来做什么。我只是希望我回答了你问题的正确部分。
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.