Skip to content

Instantly share code, notes, and snippets.

@jpfuentes2
Created April 21, 2012 01:38
Show Gist options
  • Save jpfuentes2/2433174 to your computer and use it in GitHub Desktop.
Save jpfuentes2/2433174 to your computer and use it in GitHub Desktop.
client WS
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
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