如何与Ruby中的线程通信?

时间:2020-11-26 22:22:52

I'm building a real time game, mostly chat based, and I need to have many of these chats running concurrently, receiving and sending data through web sockets.

我正在构建一个实时游戏,主要是基于聊天的,我需要让这些聊天同时运行,通过web sockets接收和发送数据。

I have been told that instead of spawning one process per game, I should have one process with one thread per game (maybe using Event Machine).

我被告知,我应该在每个游戏中使用一个线程(可能使用事件机器),而不是在每个游戏中生成一个进程。

I'm using Juggernaut for the sockets part, it lets me send data to all the players in a game by using a publish/subscribe system: each player subscribes to one game. But how do I send data from each player to that particular game?

我使用神像为socket部分,它让我通过发布/订阅系统向游戏中的所有玩家发送数据:每个玩家订阅一个游戏。但是我如何向每个玩家发送数据到特定的游戏中呢?

I was thinking that I could send the game ID or channel ID from the client to the server, and then send it to the corresponding thread.

我想我可以把游戏ID或通道ID从客户端发送到服务器,然后发送到相应的线程。

But how do I send anything to a thread?

但是,我如何将任何东西发送到线程呢?

1 个解决方案

#1


4  

To send data to a thread, you can use Ruby Queue:

要向线程发送数据,可以使用Ruby队列:

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/thread/rdoc/Queue.html

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/thread/rdoc/Queue.html

#1


4  

To send data to a thread, you can use Ruby Queue:

要向线程发送数据,可以使用Ruby队列:

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/thread/rdoc/Queue.html

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/thread/rdoc/Queue.html