Skip to content
This repository has been archived by the owner on Jan 6, 2018. It is now read-only.

Commit

Permalink
mini websocket chat
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaseger committed Mar 8, 2012
1 parent 5da06e8 commit 01fc6cd
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 36 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions config.ru
Original file line number Diff line number Diff line change
@@ -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'
}
60 changes: 60 additions & 0 deletions websocket_client.rb
Original file line number Diff line number Diff line change
@@ -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)
}
71 changes: 40 additions & 31 deletions websockets_app.rb
Original file line number Diff line number Diff line change
@@ -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.