Created
September 12, 2012 19:21
-
-
Save kyubuns/3709251 to your computer and use it in GitHub Desktop.
ちゃっと。
This file contains hidden or 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
$ -> | |
if not "WebSocket" in window | |
alert "WebSocket NOT supported by your Browser!" | |
return | |
#-----ここからライブラリでやる----- | |
METHOD_IDS = { | |
'chatStoC' : 100, | |
'chatCtoS' : 200 | |
} | |
class Connection | |
constructor: (host) -> | |
@method_caller = {} | |
@method_caller[METHOD_IDS['chatStoC']] = (args) => | |
return if args.length isnt 2 | |
@chatStoC(args[0], args[1]) | |
socket = new WebSocket host | |
socket.binaryType = 'arraybuffer' | |
socket.onopen = (evt) => @onopen(evt) | |
socket.onclose = (evt) => @onclose(evt) | |
socket.onerror = (evt) => @onerror(evt) | |
socket.onmessage = (evt) => | |
tmp = msgpack.unpack new Uint8Array evt.data | |
id = tmp[0] | |
args = tmp[1] | |
@method_caller[id](args) if @method_caller[id] | |
#call | |
@chatCtoS = (name, msg) => | |
binary = msgpack.pack [METHOD_IDS['chatCtoS'], [name, msg]] | |
arraybuffer = new Uint8Array binary | |
socket.send arraybuffer.buffer | |
#callback | |
onopen: (evt) -> | |
onclose: (evt) -> | |
onerror: (evt) -> | |
chatStoC: (name, message) -> | |
#-----ここまでライブラリでやる----- | |
class Client extends Connection | |
onopen: (evt) -> | |
$('#chat').prepend 'connected' | |
onclose: (evt) -> | |
$('#chat').prepend 'clooooooooooooooooooooooose' | |
chatStoC: (name, message) -> | |
$('#chat').prepend "<p>#{name} : #{message}</p>" | |
client = new Client "ws://localhost:9998" | |
$('#send').click -> | |
return if $('#message').val() is '' | |
client.chatCtoS($('#name').val(), $('#message').val()) | |
$('#message').val "" |
This file contains hidden or 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
# coding: utf-8 | |
require 'bundler/setup' | |
require 'em-websocket' | |
require 'msgpack' | |
#↓pullrequest送ってる | |
module EventMachine::WebSocket | |
class Connection | |
def send_binary(data) | |
if @handler | |
@handler.send_frame(:binary, data) | |
else | |
raise WebSocketError, "Cannot send binary before onopen callback" | |
end | |
end | |
end | |
end | |
#--------------ここからライブラリでやる----------------- | |
module WakuRPC | |
METHOD_IDS = { | |
'chatStoC' => 100, | |
'chatCtoS' => 200 | |
} | |
class Connection | |
def initialize(ws) | |
@method_caller = {} | |
@method_caller[METHOD_IDS['chatCtoS']] = lambda { |args| | |
if args.length != 2 or args[0].class != String or args[1].class != String then | |
return | |
end | |
chatCtoS(args[0], args[1]) | |
} | |
@ws = ws | |
@ws.onmessage { |msg| receive_message(msg) } | |
@ws.onclose { onclose() } | |
@ws.onerror { onerror() } | |
onopen() | |
end | |
private | |
def receive_message(msg) | |
tmp = MessagePack.unpack(msg) | |
id = tmp[0] | |
args = tmp[1] | |
@method_caller[id].call(args) if @method_caller.has_key?(id) | |
end | |
def send(msg) | |
@ws.send_binary msg.to_msgpack | |
end | |
# 呼び出し | |
public | |
def chatStoC(name, msg) | |
send([METHOD_IDS['chatStoC'], [name, msg]]) | |
end | |
# オーバーライドする | |
def onopen | |
end | |
def onclose | |
end | |
def onerror | |
end | |
def chatCtoS(name, msg) | |
end | |
end | |
class Server | |
def initialize(host, port, connection) | |
@host = host | |
@port = port | |
@connection = connection | |
end | |
def start | |
EventMachine::WebSocket.start(:host => @host, :port => @port) do |ws| | |
ws.onopen { @connection.new(ws) } | |
end | |
end | |
end | |
end | |
#--------------ここまでライブラリでやる----------------- | |
class Client < WakuRPC::Connection | |
class << self | |
# グローバル変数死滅 | |
def connections | |
@connections ||= {} | |
end | |
def inherited(subclass) | |
subclass.instance_variable_set :@connections, {} | |
end | |
end | |
def connections | |
self.class.connections | |
end | |
def onopen | |
connections[self] = self | |
p "someone comes." | |
p "#{connections.length} people are connecting." | |
chatStoC('server', 'aaaaaaaaaaaaaa') | |
end | |
def onclose | |
connections.delete self | |
p "someone leaves." | |
p "#{connections.length} people are connecting." | |
end | |
def chatCtoS(name, msg) | |
p "receive chatCtoS: " + name + " / " + msg | |
connections.each { |key, con| con.chatStoC(name, msg) } | |
end | |
end | |
WakuRPC::Server.new('localhost', 9998, Client).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment