Sha256: e09b9a3bbc67106b373a1d39cd9cb75c8e31302d864d35b78b92c558d2f43afa

Contents?: true

Size: 805 Bytes

Versions: 1

Compression:

Stored size: 805 Bytes

Contents

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

require 'rubygems'
require 'rev/websocket'
require 'msgpack/rpc'
require 'json'

$sockets = {}

class MyConnection < Rev::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 = Rev::Loop.default

ws = Rev::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
rev-websocket-0.1.3 examples/rpc.rb