ruby - How do I associate an Activerecord Object with Em-Websocket connection? -


i new ruby. trying implement chat client using em-websocket. have following code:

  eventmachine::websocket.start(host: '0.0.0.0', port: 8080) |websock|     websock.onopen       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) # doesn't work       # subscribe new user grindserver.realtime_channel callback function push action       new_user = grindserver.realtime_channel.subscribe { |msg| websock.send msg }       grindserver.online_people << person       # add new user user list       @users[websock.object_id] = new_user        # push last messages user       # message.all.each |message|         # websock.send message.to_json       # end       # puts grindserver.realtime_channel.inspect       # broadcast notification users       onlinepeople = []       grindserver.online_people.each |onli|         onlinepeople << person       end        # send last 10 messages 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 

basically trying maintain list of person (activerecord object) , corresponding websocket::connection object.

server code

migration

update: if unable merge. should able attach note websocket belongs person named "x"?

i solved using hash.

  eventmachine::websocket.start(host: '0.0.0.0', port: 8080) |websock|     websock.onopen       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 new user grindserver.realtime_channel callback function 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} # solves       # add new user user list       @users[websock.object_id] = new_user        onlinepeople = []       grindserver.online_people.each |onli|         onlinepeople << onli.except(:websocket)       end        # send last 10 messages 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 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -