This repository has been archived by the owner on Jan 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5da06e8
commit 01fc6cd
Showing
5 changed files
with
118 additions
and
36 deletions.
There are no files selected for viewing
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
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
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
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' | ||
} |
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
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) | ||
} |
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
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 | ||
|