如何将 Activerecord 对象与 Em-Websocket 连接相关联?
How do I associate an Activerecord Object with Em-Websocket connection?
我是 ruby 的新手。我正在尝试使用 em-websocket 实现聊天客户端。我有以下代码:
EventMachine::WebSocket.start(host: '0.0.0.0', port: 8080) do |websock|
websock.onopen do
puts 'New Connection Opened'
cookies = CGI::Cookie::parse( websock.request["cookie"])
person = Person.where(['token = ?', cookies["token"]]).first
unless person
websock.close(code = nil, body = {Error: "Invalid Token"}.to_json) unless person
return
end
puts "#{person.name} authenticated!"
person=person.attributes.merge(websock.attributes) # this doesn't work
# Subscribe the new user to the GrindServer.realtime_channel with the callback function for the push action
new_user = GrindServer.realtime_channel.subscribe { |msg| websock.send msg }
GrindServer.online_people << person
# Add the new user to the user list
@users[websock.object_id] = new_user
# Push the last messages to the user
# message.all.each do |message|
# websock.send message.to_json
# end
# puts GrindServer.realtime_channel.inspect
# Broadcast the notification to all users
onlinepeople = []
GrindServer.online_people.each do |onli|
onlinepeople << person
end
# Send last 10 messages to the newly connected user
websock.send Message.where({ receiver_id: [0, person.id}).last(10).to_json
GrindServer.realtime_channel.push ({
'id' => 0,
'sender_id' => 0,
'messagetext' => "#{person.name} joined. <$<^<#<#{@users.length}>#>^>$> users in chat",
'users' => onlinepeople,
'metadata' => websock.request["query"]["id"],
}.to_json)
end
...# other event handlers
end
基本上,我试图维护一个 Person(ActiveRecord 对象)及其对应的 WebSocket::Connection 对象的列表。
更新:即使我无法合并。我应该可以在 websocket 上附上一条注释,它属于一个名叫 "x"?
的人
我用哈希解决了。
EventMachine::WebSocket.start(host: '0.0.0.0', port: 8080) do |websock|
websock.onopen do
puts 'New Connection Opened'
cookies = CGI::Cookie::parse( websock.request["cookie"])
person = Person.where(['token = ?', cookies["token"]]).first
unless person
websock.close(code = nil, body = {Error: "Invalid Token"}.to_json) unless person
return
end
puts "#{person.name} authenticated!"
# person=person.attributes.merge(websock.attributes)
# Subscribe the new user to the GrindServer.realtime_channel with the callback function for the push action
new_user = GrindServer.realtime_channel.subscribe { |msg| websock.send msg }
GrindServer.online_people << {:ws_oid => websock.object_id,
:websocket => websock,
:person_name => person.name,
:person_trigram => person.trigram} # this solves it
# Add the new user to the user list
@users[websock.object_id] = new_user
onlinepeople = []
GrindServer.online_people.each do |onli|
onlinepeople << onli.except(:websocket)
end
# Send last 10 messages to the newly connected user
websock.send Message.where({ receiver_id: [0, person.id]}).last(10).to_json
GrindServer.realtime_channel.push ({
'id' => 0,
'sender_id' => 0,
'messagetext' => "#{person.name} joined. <$<^<#<#{@users.length}>#>^>$> users in chat",
'users' => onlinepeople,
'metadata' => person.trigram,
}.to_json)
end
我是 ruby 的新手。我正在尝试使用 em-websocket 实现聊天客户端。我有以下代码:
EventMachine::WebSocket.start(host: '0.0.0.0', port: 8080) do |websock|
websock.onopen do
puts 'New Connection Opened'
cookies = CGI::Cookie::parse( websock.request["cookie"])
person = Person.where(['token = ?', cookies["token"]]).first
unless person
websock.close(code = nil, body = {Error: "Invalid Token"}.to_json) unless person
return
end
puts "#{person.name} authenticated!"
person=person.attributes.merge(websock.attributes) # this doesn't work
# Subscribe the new user to the GrindServer.realtime_channel with the callback function for the push action
new_user = GrindServer.realtime_channel.subscribe { |msg| websock.send msg }
GrindServer.online_people << person
# Add the new user to the user list
@users[websock.object_id] = new_user
# Push the last messages to the user
# message.all.each do |message|
# websock.send message.to_json
# end
# puts GrindServer.realtime_channel.inspect
# Broadcast the notification to all users
onlinepeople = []
GrindServer.online_people.each do |onli|
onlinepeople << person
end
# Send last 10 messages to the newly connected user
websock.send Message.where({ receiver_id: [0, person.id}).last(10).to_json
GrindServer.realtime_channel.push ({
'id' => 0,
'sender_id' => 0,
'messagetext' => "#{person.name} joined. <$<^<#<#{@users.length}>#>^>$> users in chat",
'users' => onlinepeople,
'metadata' => websock.request["query"]["id"],
}.to_json)
end
...# other event handlers
end
基本上,我试图维护一个 Person(ActiveRecord 对象)及其对应的 WebSocket::Connection 对象的列表。
更新:即使我无法合并。我应该可以在 websocket 上附上一条注释,它属于一个名叫 "x"?
的人我用哈希解决了。
EventMachine::WebSocket.start(host: '0.0.0.0', port: 8080) do |websock|
websock.onopen do
puts 'New Connection Opened'
cookies = CGI::Cookie::parse( websock.request["cookie"])
person = Person.where(['token = ?', cookies["token"]]).first
unless person
websock.close(code = nil, body = {Error: "Invalid Token"}.to_json) unless person
return
end
puts "#{person.name} authenticated!"
# person=person.attributes.merge(websock.attributes)
# Subscribe the new user to the GrindServer.realtime_channel with the callback function for the push action
new_user = GrindServer.realtime_channel.subscribe { |msg| websock.send msg }
GrindServer.online_people << {:ws_oid => websock.object_id,
:websocket => websock,
:person_name => person.name,
:person_trigram => person.trigram} # this solves it
# Add the new user to the user list
@users[websock.object_id] = new_user
onlinepeople = []
GrindServer.online_people.each do |onli|
onlinepeople << onli.except(:websocket)
end
# Send last 10 messages to the newly connected user
websock.send Message.where({ receiver_id: [0, person.id]}).last(10).to_json
GrindServer.realtime_channel.push ({
'id' => 0,
'sender_id' => 0,
'messagetext' => "#{person.name} joined. <$<^<#<#{@users.length}>#>^>$> users in chat",
'users' => onlinepeople,
'metadata' => person.trigram,
}.to_json)
end