em-websocket 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gemspec.homepage = "http://github.com/igrigorik/em-websocket"
11
11
  gemspec.authors = ["Ilya Grigorik"]
12
12
  gemspec.add_dependency("eventmachine", ">= 0.12.9")
13
+ gemspec.add_dependency("addressable")
13
14
  gemspec.rubyforge_project = "em-websocket"
14
15
  end
15
16
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.4
data/examples/test.html CHANGED
@@ -11,6 +11,7 @@
11
11
  ws.onopen = function() {
12
12
  debug("connected...");
13
13
  ws.send("hello server");
14
+ ws.send("hello again");
14
15
  };
15
16
  });
16
17
  </script>
@@ -1,5 +1,5 @@
1
1
  require 'addressable/uri'
2
- require 'uri'
2
+
3
3
  module EventMachine
4
4
  module WebSocket
5
5
  class Connection < EventMachine::Connection
@@ -7,9 +7,11 @@ module EventMachine
7
7
  PATH = /^GET (\/[^\s]*) HTTP\/1\.1$/
8
8
  HEADER = /^([^:]+):\s*([^$]+)/
9
9
 
10
+ attr_reader :state
11
+
10
12
  # define WebSocket callbacks
11
13
  def onopen(&blk); @onopen = blk; end
12
- def onclose(&blk); @onclode = blk; end
14
+ def onclose(&blk); @onclose = blk; end
13
15
  def onmessage(&blk); @onmessage = blk; end
14
16
 
15
17
  def initialize(options)
@@ -31,6 +33,8 @@ module EventMachine
31
33
 
32
34
  def unbind
33
35
  debug [:unbind, :connection]
36
+
37
+ @state = :closed
34
38
  @onclose.call if @onclose
35
39
  end
36
40
 
@@ -107,10 +111,16 @@ module EventMachine
107
111
  end
108
112
 
109
113
  def process_message
114
+ return if not @onmessage
110
115
  debug [:message, @data]
111
- @onmessage.call(@data) if @onmessage
112
- @data = ''
113
116
 
117
+ # slice the message out of the buffer and pass in
118
+ # for processing, and buffer data otherwise
119
+ while msg = @data.slice!(/\000([^\377]*)\377/)
120
+ msg.gsub!(/^\x00|\xff$/, '')
121
+ @onmessage.call(msg)
122
+ end
123
+
114
124
  false
115
125
  end
116
126
 
@@ -26,8 +26,6 @@ describe EventMachine::WebSocket do
26
26
  puts "WebSocket connection open"
27
27
  ws.send MSG
28
28
  }
29
-
30
- # TODO: need .terminate method on EM-http to invoke & test .onclose callback
31
29
  end
32
30
  end
33
31
  end
@@ -47,4 +45,55 @@ describe EventMachine::WebSocket do
47
45
  end
48
46
  end
49
47
 
48
+ it "should split multiple messages into separate callbacks" do
49
+ EM.run do
50
+ messages = %w[1 2]
51
+ recieved = []
52
+
53
+ EventMachine.add_timer(0.1) do
54
+ http = EventMachine::HttpRequest.new('ws://127.0.0.1:8080/').get :timeout => 0
55
+ http.errback { failed }
56
+ http.stream {|msg|}
57
+ http.callback {
58
+ http.response_header.status.should == 101
59
+ http.send messages[0]
60
+ http.send messages[1]
61
+ }
62
+ end
63
+
64
+ EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
65
+ ws.onopen {}
66
+ ws.onclose {}
67
+ ws.onmessage {|msg|
68
+ msg.should == messages[recieved.size]
69
+ recieved.push msg
70
+
71
+ EventMachine.stop if recieved.size == messages.size
72
+ }
73
+ end
74
+ end
75
+ end
76
+
77
+ it "should call onclose callback when client closes connection" do
78
+ EM.run do
79
+ EventMachine.add_timer(0.1) do
80
+ http = EventMachine::HttpRequest.new('ws://127.0.0.1:8080/').get :timeout => 0
81
+ http.errback { failed }
82
+ http.callback {
83
+ http.response_header.status.should == 101
84
+ http.close_connection
85
+ }
86
+ http.stream{|msg|}
87
+ end
88
+
89
+ EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
90
+ ws.onopen {}
91
+ ws.onclose {
92
+ ws.state.should == :closed
93
+ EventMachine.stop
94
+ }
95
+ end
96
+ end
97
+ end
98
+
50
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-websocket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Grigorik
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-23 00:00:00 -05:00
12
+ date: 2009-12-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.12.9
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: addressable
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
25
35
  description: EventMachine based WebSocket server
26
36
  email: ilya@igvita.com
27
37
  executables: []