Created
April 21, 2012 01:38
-
-
Save jpfuentes2/2433174 to your computer and use it in GitHub Desktop.
client WS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/setup' | |
Bundler.setup | |
require "eventmachine" | |
require "em-websocket" | |
EM.run do | |
channel = EM::Channel.new | |
EM::WebSocket.start(host: "0.0.0.0", port: 2092, debug: true) do |ws| | |
ws.onopen do | |
sid = channel.subscribe { |msg| ws.send msg } | |
ws.onclose { channel.unsubscribe(sid) } | |
end | |
end | |
socket = context.socket(ZMQ::PULL, PullHandler.new(channel)) | |
socket.connect('tcp://127.0.0.1:2091') | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Websocket | |
constructor: (@options) -> | |
@start() | |
start: -> | |
window.WebSocket = window.MozWebSocket if window.WebSocket == undefined and window.MozWebSocket != undefined | |
@socket = new window.WebSocket @options.url | |
@socket.onmessage = (e) => @onmessage e | |
@socket.onclose = (e) => @onclose e | |
onmessage: (e) -> | |
data = JSON.parse e.data | |
EM.trigger "websockets:onmessage", data | |
onclose: (e) -> @reconnect() | |
reconnect: -> | |
clearTimeout @_reconnect | |
@_reconnect = setTimeout => | |
@start() | |
, 2000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment