rev-websocket 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,4 +1,9 @@
1
1
 
2
+ 2010-07-22 version 0.1.3:
3
+
4
+ * rescue all StandardErrors on_readable
5
+ * adds 'RPC push' example
6
+
2
7
  2010-07-17 version 0.1.2:
3
8
 
4
9
  * sends <cross-domain-policy> on <policy-file-request> to accept Flash-based clients
@@ -15,6 +15,12 @@ This library conforms to WebSocket draft-75 and draft-76.
15
15
 
16
16
  == Examples
17
17
 
18
+ - examples/echo.rb - receives a message from clients and just echos.
19
+ - examples/shoutchat.rb - simple chat application.
20
+ - examples/rpc.rb - pushes messages to browsers from programs separated from the web socket server.
21
+
22
+ See examples/README.md[http://github.com/frsyuki/rev-websocket/tree/master/examples/] for details.
23
+
18
24
  === Simple echo server
19
25
 
20
26
  require 'rubygems'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -14,11 +14,27 @@ A HTTP server runs on localhost:8080 and WebSocket server runs on localhost:8081
14
14
  Then access to [htt://localhost:8080/echo.html](http://localhost:8080/echo.html).
15
15
 
16
16
 
17
+ ## RPC push
18
+
19
+ With RPC (Remote Procedure Call), you can push messages to browsers from programs separated from the WebSocket server.
20
+
21
+ In this example, a Sinatra based web appliction pushes messages using MessagePack-RPC, a simple cross-language RPC library.
22
+
23
+ $ gem install msgpack-rpc
24
+ $ gem install rev-websocket
25
+ $ gem install sinatra
26
+ $ gem install json
27
+ $ ruby ./rpc
28
+
29
+ Then access to [htt://localhost:8080/](http://localhost:8080/).
30
+
31
+
17
32
  ## ShoutChat
18
33
 
19
34
  ShoutChat is a simple browser-based chat application.
20
35
 
21
36
  $ gem install rev-websocket
37
+ $ gem install json
22
38
  $ ruby ./shoutchat
23
39
 
24
40
  Then access to [htt://localhost:8080/shoutchat.html](http://localhost:8080/shoutchat.html).
@@ -5,7 +5,7 @@ require 'rev/websocket'
5
5
 
6
6
  class EchoConnection < Rev::WebSocket
7
7
  def on_open
8
- puts "WebSocket opened"
8
+ puts "WebSocket opened from '#{peeraddr[2]}': request=#{request.inspect}"
9
9
  send_message("server: Hello, world!")
10
10
  end
11
11
 
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ fork {
4
+ load 'rpc.rb'
5
+ exit 0
6
+ }
7
+
8
+ require 'rubygems'
9
+ require 'sinatra'
10
+ require 'msgpack/rpc'
11
+
12
+ rpc_port = 18800
13
+ $ws = MessagePack::RPC::Client.new('127.0.0.1', rpc_port)
14
+
15
+ get '/' do
16
+ erb :rpc
17
+ end
18
+
19
+ post '/push' do
20
+ data = {'Hello'=>'World!', 'data' => params}
21
+ $ws.call(:push_data, data)
22
+ redirect '/', 303
23
+ end
24
+
25
+ set :port, 8080
26
+
@@ -0,0 +1,47 @@
1
+ # RPC push
2
+ # This program receives messages.
3
+ # See ./rpc file which sends messages to this program.
4
+
5
+ require 'rubygems'
6
+ require 'rev/websocket'
7
+ require 'msgpack/rpc'
8
+ require 'json'
9
+
10
+ $sockets = {}
11
+
12
+ class MyConnection < Rev::WebSocket
13
+ def on_open
14
+ puts "WebSocket opened from '#{peeraddr[2]}': request=#{request.inspect}"
15
+ $sockets[self] = self
16
+ end
17
+
18
+ def on_close
19
+ puts "WebSocket closed"
20
+ $sockets.delete(self)
21
+ end
22
+ end
23
+
24
+ class RPCServer
25
+ def push_data(data)
26
+ $sockets.each_key {|sock|
27
+ sock.send_message(data.to_json)
28
+ }
29
+ nil
30
+ end
31
+ end
32
+
33
+ host = '0.0.0.0'
34
+ port = ARGV[0] || 8081
35
+
36
+ rpc_port = 18800
37
+
38
+ loop = Rev::Loop.default
39
+
40
+ ws = Rev::WebSocketServer.new(host, port, MyConnection)
41
+ ws.attach(loop)
42
+
43
+ rpc = MessagePack::RPC::Server.new(loop)
44
+ rpc.listen('127.0.0.1', rpc_port, RPCServer.new)
45
+
46
+ loop.run
47
+
@@ -41,8 +41,7 @@ $record = []
41
41
 
42
42
  class ShoutChatConnection < Rev::WebSocket
43
43
  def on_open
44
- @host = request['HTTP_HOST']
45
- return unless @host
44
+ @host = peeraddr[2]
46
45
  puts "connection opened: <#{@host}>"
47
46
 
48
47
  @sid = $pubsub.subscribe {|data|
@@ -53,7 +52,7 @@ class ShoutChatConnection < Rev::WebSocket
53
52
  end
54
53
 
55
54
  def on_message(data)
56
- puts "broadcasting: <#{@host}> #{data}"
55
+ puts "broadcasting: <#{@host}> '#{data}'"
57
56
 
58
57
  $pubsub.publish(data)
59
58
  $record.push(data)
@@ -61,7 +60,6 @@ class ShoutChatConnection < Rev::WebSocket
61
60
  end
62
61
 
63
62
  def on_close
64
- return unless @host
65
63
  puts "connection closed: <#{@host}>"
66
64
 
67
65
  $pubsub.unsubscribe(@sid)
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta content="text/css" http-equiv="content-style-type" />
6
+ <meta content="text/javascript" http-equiv="content-script-type" />
7
+
8
+ <script type="text/javascript" src='js/jquery.min.js'></script>
9
+ <script type="text/javascript" src='js/swfobject.js'></script>
10
+ <script type="text/javascript" src='js/FABridge.js'></script>
11
+ <script type="text/javascript" src='js/web_socket.js'></script>
12
+ <script type="text/javascript" src='js/json2.js'></script>
13
+ <link rel="stylesheet" type="text/css" href="echo.css" />
14
+
15
+ <title>Rev-WebSocket Demo: Echo server</title>
16
+
17
+ <script>
18
+ WS_URL = "ws://localhost:8081";
19
+ WEB_SOCKET_SWF_LOCATION = "js/WebSocketMain.swf";
20
+
21
+ var global = this;
22
+
23
+ $(document).ready(function(){
24
+
25
+ function debug(message) {
26
+ html = "<p><span class='time'>"+new Date()+"</span>"+message+"</p>"
27
+ $("#debug").append(html);
28
+ }
29
+
30
+ debug("connecting to "+WS_URL+"...");
31
+ ws = new WebSocket(WS_URL);
32
+
33
+ ws.onopen = function() {
34
+ debug("connected.");
35
+
36
+ text = "client: hello";
37
+ ws.send(text);
38
+
39
+ debug("message sent: "+text);
40
+ }
41
+
42
+ ws.onclose = function() {
43
+ debug("disconnected...");
44
+ }
45
+
46
+ ws.onerror = function(msg) {
47
+ debug("failed to connect:"+msg);
48
+ }
49
+
50
+ ws.onmessage = function(event) {
51
+ debug("message received: "+event.data);
52
+ $("#message").append("<p>"+event.data+"</p>");
53
+ }
54
+ });
55
+ </script>
56
+ </head>
57
+ <body>
58
+ <div>
59
+ <form action="/push" target="_blank" method="post">
60
+ <input type="text" name="text" id="text" value="text..." />
61
+ <input type="submit" value="submit" />
62
+ </form>
63
+ </div>
64
+
65
+ <div id="message"></div>
66
+ <div id="debug"></div>
67
+ </body>
@@ -61,6 +61,12 @@ module Rev
61
61
  @request = {DummyIO::KEY => DummyIO.new}
62
62
  end
63
63
 
64
+ def on_readable
65
+ super
66
+ rescue
67
+ close
68
+ end
69
+
64
70
  def on_read(data)
65
71
  @data << data
66
72
  dispatch
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rev-websocket
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - FURUHASHI Sadayuki
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-17 00:00:00 +09:00
18
+ date: 2010-07-22 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -77,8 +77,11 @@ files:
77
77
  - examples/public/js/web_socket.js
78
78
  - examples/public/shoutchat.css
79
79
  - examples/public/shoutchat.html
80
+ - examples/rpc
81
+ - examples/rpc.rb
80
82
  - examples/shoutchat
81
83
  - examples/shoutchat.rb
84
+ - examples/views/rpc.erb
82
85
  - lib/rev-websocket.rb
83
86
  - lib/rev/websocket.rb
84
87
  - lib/rev/websocket/spec.rb
@@ -118,4 +121,5 @@ specification_version: 3
118
121
  summary: WebSocket server based on Rev
119
122
  test_files:
120
123
  - examples/echo.rb
124
+ - examples/rpc.rb
121
125
  - examples/shoutchat.rb