Sha256: 923c37a7fec0b726103b9b46dfe09e391168666872bb0d4e0da4e09684bd328f

Contents?: true

Size: 821 Bytes

Versions: 1

Compression:

Stored size: 821 Bytes

Contents

# RPC push
# This program receives messages.
# See ./rpc file which sends messages to this program.

require 'rubygems'
require 'cool.io-websocket'
require 'msgpack/rpc'
require 'json'

$sockets = {}

class MyConnection < Cool.io::WebSocket
	def on_open
		puts "WebSocket opened from '#{peeraddr[2]}': request=#{request.inspect}"
		$sockets[self] = self
	end

	def on_close
		puts "WebSocket closed"
		$sockets.delete(self)
	end
end

class RPCServer
	def push_data(data)
		$sockets.each_key {|sock|
			sock.send_message(data.to_json)
		}
		nil
	end
end

host = '0.0.0.0'
port = ARGV[0] || 8081

rpc_port = 18800

loop = Cool.io::Loop.default

ws = Cool.io::WebSocketServer.new(host, port, MyConnection)
ws.attach(loop)

rpc = MessagePack::RPC::Server.new(loop)
rpc.listen('127.0.0.1', rpc_port, RPCServer.new)

loop.run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cool.io-websocket-0.1.4 examples/rpc.rb