处理golang中的websocket通信的设计实践

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

Design practices in handling websocket communication in golang

问题

我刚开始学习Go语言,想通过替换一个Node.js服务器来学习它。我的Node.js服务器有以下组件:

  1. 它会接收来自客户端的WebSocket消息。客户端会提供一个唯一的ID和消息的密钥。服务器会根据密钥处理消息(类似于REST接口),并返回带有唯一ID的消息给客户端,以便客户端知道服务器正在响应哪条消息。

  2. 对于某些消息,服务器会生成一个外部进程(每个连接生成一个)。然后,服务器会作为生成的进程的“客户端”,发送带有唯一ID的JSON消息,并接收返回的数据。在这种情况下,服务器充当生成的进程和客户端之间的中间人。

在Node.js中,实现这个功能非常简单。我只需将生成的进程添加到我的“主”连接中,并在主函数中使用回调函数。WebSocket服务器模块和处理生成进程通信的模块之间没有直接交互,它们完全通用。

然而,在Go语言中,虽然我有一个良好工作的WebSocket包和生成进程包,但我还没有找到一种好的方法来为每个WebSocket连接通用地创建一个生成的进程。我考虑过传递回调函数和初始化函数,但这种方法似乎有些笨拙。我知道Node.js和Go语言可能需要不同的编程策略,但我无法想出一个好的“Go”方式来实现这个。我欢迎任何建议!

英文:

I am new to go and trying to learn it by replacing a nodejs server. My nodejs server had the following components:

  1. It received a websocket message from a client. The client would provide a unique ID and a key for the message. The server would process the message based on the key (much like a REST interface would) and return a message to the client with the unique ID so that the client would know
    which message the server was responding to.

  2. For some of the messages, the server would spawn an external process (one for each connection). The server would then act as a "client" to the spawned process, sending JSON messages with unique IDs and receiving back data. The server in this instance acts as an intermediary between the spawned process and the client.

On node it was trivial to get this to work generically. I simply added the spawned process to the connection in my "main" and used callbacks in the main. The websocket server module and the module governing the spawned process communication had no direct interaction and were completely generic.

However, while I have a websocket package and a spawn package that work well in go, I haven't been able to figure out a good way to generically create a spawned process per websocket connection. I've thought of passing callbacks and inits but this method seems hackish. I recognize that node and go probably require different strategies in programming but I can't think of a good "go" way of doing this. I welcome any suggestions!

答案1

得分: 1

  1. 你描述了HTTP协议的工作原理,即请求-响应。如果这是你所需要的,你可以使用持久连接(keep-alive connections)。WebSocket是异步的,消息不需要响应。例如,服务器发送事件(server-send-events)是一种使用情况。
  2. 在Go中,服务器使用了“发出并忘记”(fire-and-forget)的策略。它接受连接,生成一个goroutine来处理它,然后忘记它并监听新的连接。生成的goroutine将连接升级为WebSocket,然后直接为客户端提供服务,没有任何中间人。
英文:
  1. You describe how http protocol works, means request-response. If that all you need you may feel good with just keep-alive connections. Websocket is async in nature, messages aren't assume responses. Use-case is server-send-events for example.
  2. In Go server uses fire-and-forget strategy. It accepts connection, spawns goroutine to handle it, forgets it and listens for a new. Spawned goroutine upgrades connection to websocket and then serves client itself without any intermediary.

huangapple
  • 本文由 发表于 2016年9月12日 00:51:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/39438422.html
匿名

发表评论

匿名网友

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

确定