Permalink
Browse files

Pulled changes from sidonath to handle newer protocol version 76. Upd…

…ated readme, adding support for batched messages from client.
  • Loading branch information...
1 parent 6e4e62c commit 20a47fe1f1bf9eb9845ed5918c190e4caf626e31 @dansimpson committed Jun 17, 2010
Showing with 340 additions and 343 deletions.
  1. +1 −2 .gitignore
  2. +56 −63 README.markdown
  3. +12 −11 lib/web_socket.rb
  4. +70 −68 lib/web_socket/client.rb
  5. +201 −199 lib/web_socket/server.rb
View
@@ -1,3 +1,2 @@
*.gem
-*.gemspec
-
+.DS_Store
View
@@ -1,63 +1,56 @@
-#em-websocket-server
-
-* em-websocket-server allows the creation of efficient, evented, websocket services
-
-##Installation
-
-If you don't have gemcutter
-
- gem install gemcutter
- gem tumble
-
-Otherwise
-
- gem install em-websocket-server
-
-Or
-
- gem install em-websocket-server -s http://gemcutter.org
-
-##Dependencies
-- eventmachine http://github.com/eventmachine/eventmachine
-
-##Docs
-
-Not yet... coming soon
-
-##Quick Example
-
- require 'rubygems'
- require 'em-websocket-server'
- require 'json'
-
- #create a channel for pub sub
- $chatroom = EM::Channel.new
-
- class ChatServer < WebSocket::Server
-
- #subscribe to the channel on client connect
- def on_connect
- @sid = $chatroom.subscribe do |msg|
- send_message msg
- end
- end
-
- #unsubscribe on client disconnect
- def on_disconnect
- $chatroom.unsubscribe(@sid)
- end
-
- #publish the message to the channel on
- #client message received
- def on_receive msg
- $chatroom.push msg
- end
-
- end
-
- #start the event machine on port 8000 and have
- #it instantiate ChatServer objects for each
- #client connection
- EM.run do
- EM.start_server "0.0.0.0", 8000, ChatServer
- end
+#em-websocket-server
+
+* em-websocket-server allows the creation of efficient, evented, websocket services
+
+##Installation
+
+ gem install em-websocket-server -s http://gemcutter.org
+
+##Dependencies
+- eventmachine http://github.com/eventmachine/eventmachine
+
+##Docs
+
+Not yet... coming soon
+
+##Quick Example
+
+ require 'rubygems'
+ require 'web_socket'
+ require 'json'
+
+ #create a channel for pub sub
+ $chatroom = EM::Channel.new
+
+ class ChatServer < WebSocket::Server
+
+ #subscribe to the channel on client connect
+ def on_connect
+ @sid = $chatroom.subscribe do |msg|
+ send_message msg
+ end
+ end
+
+ #unsubscribe on client disconnect
+ def on_disconnect
+ $chatroom.unsubscribe(@sid)
+ end
+
+ #publish the message to the channel on
+ #client message received
+ def on_receive msg
+ $chatroom.push msg
+ end
+
+ end
+
+ #start the event machine on port 8000 and have
+ #it instantiate ChatServer objects for each
+ #client connection
+ EM.run do
+ EM.start_server "0.0.0.0", 8000, ChatServer
+ end
+
+##Thanks
+sidonath
+TheBreeze
View
@@ -1,12 +1,13 @@
-require 'rubygems'
-require 'eventmachine'
-require 'digest'
-require 'pp'
-
-module WebSocket
-end
-
-require 'web_socket/util.rb'
-require 'web_socket/frame.rb'
-require 'web_socket/server.rb'
+require 'rubygems'
+require 'eventmachine'
+require 'digest'
+require 'pp'
+
+module WebSocket
+ VERSION = 0.13
+end
+
+require 'web_socket/util.rb'
+require 'web_socket/frame.rb'
+require 'web_socket/server.rb'
require 'web_socket/client.rb'
View
@@ -1,69 +1,71 @@
-
-module WebSocket
-
- class Client < EM::Connection
-
- def path
- "/chat"
- end
-
- def host
- "localhost:8000"
- end
-
- def origin
- "localhost"
- end
-
- # em override
- def post_init
- @connected = false
- end
-
- def connection_completed
- send_headers
- end
-
- # em override
- def unbind
- on_disconnect
- end
-
- def on_disconnect
- end
-
- def send_message msg
- send_data Frame.encode(msg)
- end
-
- private
-
- def receive_data data
- unless @connected
- handshake data
- else
- on_receive Frame.decode(data)
- end
- end
-
- def handshake data
-
- #convert the headers to a hash
- @headers = Util.parse_headers(data)
- @connected = true
-
- on_connect
- end
-
- def send_headers
- result = "GET #{path} HTTP/1.1\r\n"
- result << "Upgrade: WebSocket\r\n"
- result << "Connection: Upgrade\r\n"
- result << "Host: #{host}\r\n"
- result << "Origin: #{origin}\r\n\r\n"
-
- send_data result
- end
- end
-
+
+module WebSocket
+
+ class Client < EM::Connection
+
+ def path
+ "/chat"
+ end
+
+ def host
+ "localhost:8000"
+ end
+
+ def origin
+ "localhost"
+ end
+
+ # em override
+ def post_init
+ @connected = false
+ end
+
+ def connection_completed
+ send_headers
+ end
+
+ # em override
+ def unbind
+ on_disconnect
+ end
+
+ def on_disconnect
+ end
+
+ def send_message msg
+ send_data Frame.encode(msg)
+ end
+
+ private
+
+ def receive_data data
+ unless @connected
+ handshake data
+ else
+ while msg = data.slice!(/\000([^\377]*)\377/)
+ on_receive Frame.decode(msg)
+ end
+ end
+ end
+
+ def handshake data
+
+ #convert the headers to a hash
+ @headers = Util.parse_headers(data)
+ @connected = true
+
+ on_connect
+ end
+
+ def send_headers
+ result = "GET #{path} HTTP/1.1\r\n"
+ result << "Upgrade: WebSocket\r\n"
+ result << "Connection: Upgrade\r\n"
+ result << "Host: #{host}\r\n"
+ result << "Origin: #{origin}\r\n\r\n"
+
+ send_data result
+ end
+ end
+
end
Oops, something went wrong.

0 comments on commit 20a47fe

Please sign in to comment.