也许这是服务器推送系统的一个很好的例子。系统中有很多用户,用户可以互相交谈。可以这样实现:一个用户(通过websocket)向服务器发送消息,然后服务器将消息转发给另一个用户。关键是找到ws(websocket对象)和用户之间的绑定。下面的示例代码如下:
EM.run {
EM::WebSocket.run(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws|
ws.onopen { |handshake|
# extract the user id from handshake and store the binding between user and ws
}
ws.onmessage { |msg|
# extract the text and receiver id from msg
# extract the ws_receiver from the binding
ws_receiver.send(text)
}
end
}我想找出以下问题:
ws对象,以便将其存储到磁盘或数据库中?否则,我只能将绑定存储到内存中。发布于 2015-12-29 06:33:58
您正在接近一个websockets非常适合的用例,因此您走上了正确的轨道。
ws对象,但是将websocket对象看作有点像http请求对象,因为它们是一种通信类型的抽象。最好是封送/存储数据。发布于 2015-12-29 17:36:02
以下是对蔡斯·吉利亚姆简洁的回答的补充,其中包括对em-websocket、websocket-rails (很长一段时间没有维护)、faye-websocket-rails和ActionCable的引用。
我建议使用普莱齐框架。它既是一个独立的应用程序框架,也是一个Rails Websocket增强。
我也会考虑以下几点:
这些点将帮助您决定是否对消息使用持久存储(即数据库)。
也就是说,要在Rails中使用普莱齐,请在应用程序的config/initializers文件夹中创建一个init_plezi.rb。使用以下代码(作为示例):
class ChatDemo
# use JSON events instead of raw websockets
@auto_dispatch = true
protected #protected functions are hidden from regular Http requests
def auth msg
@user = User.auth_token(msg['token'])
return close unless @user
# creates a websocket "mailbox" that will remain open for 9 hours.
register_as @user.id, lifetime: 60*60*9, max_connections: 5
end
def chat msg, received = false
unless @user # require authentication first
close
return false
end
if received
# this is only true when we sent the message
# using the `broadcast` or `notify` methods
write msg # writes to the client websocket
end
msg['from'] = @user.id
msg['time'] = Plezi.time # an existing time object
unless msg['to'] && registered?(msg['to'])
# send an error message event
return {event: :err, data: 'No recipient or recipient invalid'}.to_json
end
# everything was good, let's send the message and inform
# this will invoke the `chat` event on the other websocket
# notice the `true` is setting the `received` flag.
notify msg['to'], :chat, msg, true
# returning a String will send it to the client
# when using the auto-dispatch feature
{event: 'message_sent', msg: msg}.to_json
end
end
# remember our route for websocket connections.
route '/ws_chat', ChatDemo
# a route to the Javascript client (optional)
route '/ws/client.js', :client请设置自己的服务器(碘,Ruby服务器),所以请记住从应用程序中删除对puma、thin或任何其他自定义服务器的引用。
在客户端,您可能希望使用Plezi提供的Javascript助手(它是可选的).加:
<script src='/es/client.js' />
<script>
TOKEN = <%= @user.token %>;
c = new PleziClient(PleziClient.origin + "/ws_chat") // the client helper
c.log_events = true // debug
c.chat = function(event) {
// do what you need to print a received message to the screen
// `event` is the JSON data. i.e.: event.event == 'chat'
}
c.error = function(event) {
// do what you need to print a received message to the screen
alert(event.data);
}
c.message_sent = function(event) {
// invoked after the message was sent
}
// authenticate once connection is established
c.onopen = function(event) {
c.emit({event: 'auth', token: TOKEN});
}
// // to send a chat message:
// c.emit{event: 'chat', to: 8, data: "my chat message"}
</script>我没有测试实际的消息代码,因为它只是一个框架,而且它还需要一个带有User模型和token的Rails应用程序,我不想仅仅为了回答一个问题而编辑这个应用程序(无意冒犯)。
https://stackoverflow.com/questions/34505574
复制相似问题