Permalink
Browse files

mini websocket chat

  • Loading branch information...
1 parent 5da06e8 commit 01fc6cd51d8a49f99800c62e1d04e762b0c38ed8 @andreaseger committed Mar 8, 2012
Showing with 118 additions and 36 deletions.
  1. +3 −0 Gemfile
  2. +10 −0 Gemfile.lock
  3. +5 −5 config.ru
  4. +60 −0 websocket_client.rb
  5. +40 −31 websockets_app.rb
View
@@ -9,6 +9,9 @@ gem 'mustache', :require => 'mustache/sinatra'
gem 'redis'
gem 'thin'
gem 'em-websocket'
+gem 'em-http-request'
+gem 'msgpack'
+gem 'json'
group :assets do
gem 'therubyracer'
View
@@ -23,9 +23,14 @@ GEM
compass (>= 0.11.1)
daemons (1.1.8)
diff-lcs (1.1.3)
+ em-http-request (0.3.0)
+ addressable (>= 2.0.0)
+ escape_utils
+ eventmachine (>= 0.12.9)
em-websocket (0.3.6)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
+ escape_utils (0.2.4)
eventmachine (0.12.10)
execjs (1.3.0)
multi_json (~> 1.0)
@@ -41,11 +46,13 @@ GEM
sprockets (~> 2.0)
highline (1.6.11)
hike (1.2.1)
+ json (1.6.5)
libv8 (3.3.10.4)
metaclass (0.0.1)
method_source (0.7.0)
mocha (0.10.4)
metaclass (~> 0.0.1)
+ msgpack (0.4.6)
multi_json (1.1.0)
mustache (0.99.4)
net-scp (1.0.4)
@@ -114,10 +121,13 @@ DEPENDENCIES
coffee-script
compass (>= 0.12.alpha.1)
compass-susy-plugin
+ em-http-request
em-websocket
guard-rspec
guard-sprockets2
+ json
mocha
+ msgpack
mustache
pry
rake
View
@@ -1,8 +1,8 @@
$:.unshift File.expand_path("../config",__FILE__)
require 'environment'
-require './app'
-run Rack::URLMap.new({
- '/' => App,
- '/assets' => sprockets
-})
+MESSAGEPACK = false
+
+EventMachine.run {
+ require './websockets_app'
+}
View
@@ -0,0 +1,60 @@
+require 'eventmachine'
+require 'em-http-request'
+
+MESSAGEPACK = false
+
+if MESSAGEPACK
+ require 'msgpack'
+else
+ require 'json'
+end
+
+def pack(msg)
+ if MESSAGEPACK
+ msg.to_msgpack
+ else
+ msg.to_json
+ end
+end
+
+def unpack(msg)
+ if MESSAGEPACK
+ MessagePack.unpack(msg)
+ else
+ JSON.parse(msg)
+ end
+end
+alias :p :pack
+alias :u :unpack
+
+class KeyboardHandler < EM::Connection
+ include EM::Protocols::LineText2
+
+ attr_reader :http
+
+ def initialize(h)
+ @http = h
+ end
+
+ def receive_line(data)
+ @http.send p(msg: data)
+ end
+end
+
+EventMachine.run {
+ http = EventMachine::HttpRequest.new("ws://0.0.0.0:3000").get :timeout => 0
+
+ http.errback { puts "oops" }
+ http.callback {
+ puts "WebSocket connected!"
+ http.send p(msg: "Hello client")
+ }
+
+ http.stream { |msg|
+ m = u msg
+ print "#{m['sid']}>: #{m['msg']}\n"
+ #print m['msg']
+ }
+
+ EventMachine.open_keyboard(KeyboardHandler, http)
+}
View
@@ -1,37 +1,46 @@
-EventMachine.run {
- require "./app"
- #def db
- # $redis ||= Redis.new(REDIS_CONFIG)
- #end
- #def add_client(socket)
- # db.sadd 'clients', socket.to_json
- #end
- #def remove_client(socket)
- # db.srem 'clients', socket.to_json
- #end
- #def broadcast(msg)
- # db.smembers('clients').each {|e| JSON.parse(e).send(msg) }
- #end
- @channel = EM::Channel.new
+@channel = EM::Channel.new
- EventMachine::WebSocket.start(host: '0.0.0.0', port: 3000) do |ws|
- ws.on_open {
- sid = @channel.subscribe { |msg| ws.send msg }
- @channel.push "#{sid} connected!"
+def pack(msg)
+ if MESSAGEPACK
+ msg.to_msgpack
+ else
+ msg.to_json
+ end
+end
- ws.on_close {
- @channel.unsubscribe(sid)
- @channel.push "#{sid} disconnected!"
- }
+def unpack(msg)
+ if MESSAGEPACK
+ MessagePack.unpack(msg)
+ else
+ JSON.parse(msg)
+ end
+end
+alias :p :pack
+alias :u :unpack
- ws.on_message { |msg|
- @channel.push "<#{sid}>: #{msg}"
- }
+EventMachine::WebSocket.start(host: '0.0.0.0', port: 3000) do |ws|
+ ws.onopen {
+ sid = @channel.subscribe { |msg|
+ ws.send p(msg)
+ }
+ @channel.push(sid: sid, msg: 'connected!')
- ws.on_error { |error|
- puts "Error occured: " + error.message
- }
+ ws.onclose {
+ @channel.unsubscribe(sid)
+ @channel.push(sid: sid, msg: 'disconnected!')
}
- end
-}
+ ws.onmessage { |msg|
+ m = u msg
+ @channel.push(sid: sid, msg: m['msg'])
+ }
+
+ ws.onerror { |error|
+ puts "Error occured: " + error.message
+ }
+ }
+ #EventMachine.add_periodic_timer(5) {
+ # @channel.push(sid: -1, msg: "--still alive---")
+ #}
+end
+

0 comments on commit 01fc6cd

Please sign in to comment.