2016-01-15 30 views
3

我想使用websocket-eventmachine-client gem嘗試創建多個併發web套接字,但我不確定如何去解決它。使用eventmachine設置併發web套接字

我需要使用EM.defer還是類似的東西?

例如,我有20個URL需要連接和收聽。 要連接到一個我可以這樣做:

EM.run do 
    ws = WebSocket::EventMachine::Client.connect(uri: host) 

    ws.onopen do 
    p :open 
    end 

    ws.onmessage do |msg, type| 
    p [:message, msg] 
    end 

    ws.onclose do |code, reason| 
    p :closed 
    end 

    ws.onerror do |error| 
    p :error 
    end 
end 

什麼代碼做20次?到目前爲止,我只設法連接串口。

+0

你嘗試過什麼代碼,給你的串行連接?你有沒有試着用「延期」來包裝它們? – Phrogz

+0

我想我只是包裝了連接到EM.run內的流的代碼 –

回答

0

在我到底:

EM.run do 
    EM::Iterator.new(connections_list, connections_list.size).each do |conn, iterator| 
    # Connection stuff as in question 
    end 
end