eventmachine-websocket 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown ADDED
@@ -0,0 +1,46 @@
1
+ # eventmachine-websocket
2
+
3
+ An [EventMachine](http://github.com/eventmachine/eventmachine/) based WebSocket server.
4
+
5
+ ## Example
6
+
7
+ EventMachine::WebSocket.start do |socket|
8
+ socket.open do
9
+ puts "New connection."
10
+ end
11
+ socket.message do |message|
12
+ socket.send "Received: #{message}"
13
+ end
14
+ socket.close do
15
+ puts "Closed connection."
16
+ end
17
+ end
18
+
19
+
20
+ ## Credits
21
+
22
+ Based on [em-websocket](http://github.com/igrigorik/em-websocket) by [Ilya Grigorik](http://github.com/igrigorik).
23
+
24
+ ## License
25
+
26
+ The MIT License
27
+
28
+ Copyright (c) 2010 Tristan Dunn
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining a copy
31
+ of this software and associated documentation files (the "Software"), to deal
32
+ in the Software without restriction, including without limitation the rights
33
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34
+ copies of the Software, and to permit persons to whom the Software is
35
+ furnished to do so, subject to the following conditions:
36
+
37
+ The above copyright notice and this permission notice shall be included in
38
+ all copies or substantial portions of the Software.
39
+
40
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ Spec::Rake::SpecTask.new do |task|
5
+ task.spec_opts = %w(--color --format specdoc)
6
+ end
7
+
8
+ begin
9
+ require 'jeweler'
10
+
11
+ Jeweler::Tasks.new do |gemspec|
12
+ gemspec.name = 'eventmachine-websocket'
13
+ gemspec.summary = 'An EventMachine based WebSocket server.'
14
+ gemspec.description = 'An EventMachine based WebSocket server.'
15
+ gemspec.email = 'hello@tristandunn.com'
16
+ gemspec.homepage = 'http://github.com/tristandunn/eventmachine-websocket'
17
+ gemspec.authors = ['Tristan Dunn']
18
+ end
19
+ rescue LoadError
20
+ end
21
+
22
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{eventmachine-websocket}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tristan Dunn"]
12
+ s.date = %q{2010-03-06}
13
+ s.description = %q{An EventMachine based WebSocket server.}
14
+ s.email = %q{hello@tristandunn.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "eventmachine-websocket.gemspec",
23
+ "examples/custom.rb",
24
+ "examples/echo.rb",
25
+ "lib/eventmachine-websocket.rb",
26
+ "lib/eventmachine-websocket/connection.rb",
27
+ "lib/eventmachine-websocket/websocket.rb",
28
+ "spec/spec_helper.rb",
29
+ "spec/websocket_spec.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/tristandunn/eventmachine-websocket}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.6}
35
+ s.summary = %q{An EventMachine based WebSocket server.}
36
+ s.test_files = [
37
+ "spec/spec_helper.rb",
38
+ "spec/websocket_spec.rb",
39
+ "examples/custom.rb",
40
+ "examples/echo.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ else
49
+ end
50
+ else
51
+ end
52
+ end
53
+
@@ -0,0 +1,17 @@
1
+ require 'lib/eventmachine-websocket'
2
+
3
+ class Client < EventMachine::WebSocket::Connection
4
+ def open
5
+ puts "New connection."
6
+ end
7
+
8
+ def close
9
+ puts "Lost connection."
10
+ end
11
+
12
+ def message(data)
13
+ send "Received: #{data}"
14
+ end
15
+ end
16
+
17
+ EventMachine::WebSocket.start(:klass => Client)
data/examples/echo.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'lib/eventmachine-websocket'
2
+
3
+ EventMachine::WebSocket.start do |socket|
4
+ socket.message do |message|
5
+ socket.send message
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'eventmachine'
4
+ require 'eventmachine-websocket/connection'
5
+ require 'eventmachine-websocket/websocket'
@@ -0,0 +1,114 @@
1
+ module EventMachine::WebSocket
2
+ class Connection < EventMachine::Connection
3
+ def post_init
4
+ @data = ''
5
+ @connected = false
6
+ end
7
+
8
+ def receive_data(data)
9
+ @data << data
10
+
11
+ if @connected
12
+ process_message
13
+ else
14
+ setup_connection
15
+ end
16
+ end
17
+
18
+ def unbind
19
+ close
20
+ end
21
+
22
+ def send(data)
23
+ send_data "\x00#{data}\xff"
24
+ end
25
+
26
+ def on_open(&block)
27
+ @open_callback = block
28
+ end
29
+
30
+ def on_close(&block)
31
+ @close_callback = block
32
+ end
33
+
34
+ def on_message(&block)
35
+ @message_callback = block
36
+ end
37
+
38
+ protected
39
+
40
+ def open
41
+ if @open_callback
42
+ @open_callback.call
43
+ end
44
+ end
45
+
46
+ def close
47
+ if @close_callback
48
+ @close_callback.call
49
+ end
50
+ end
51
+
52
+ def message(data)
53
+ if @message_callback
54
+ @message_callback.call(data)
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def headers
61
+ @headers ||= headers!
62
+ end
63
+
64
+ def headers!
65
+ data = @data.split("\r\n")
66
+ data.inject({}) do |result, header|
67
+ if header.match(/^([^:]+):\s*([^$]+)/)
68
+ result[$1] = $2
69
+ elsif header.match(/^GET (\/[^\s]*) HTTP\/1\.1$/)
70
+ result['Path'] = $1
71
+ end
72
+
73
+ result
74
+ end
75
+ end
76
+
77
+ def process_message
78
+ while data = @data.slice!(/\000([^\377]*)\377/)
79
+ message(data[1..-2])
80
+ end
81
+ end
82
+
83
+ def send_handshake
84
+ location = "ws://#{headers['Host']}#{headers['Path']}"
85
+ upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
86
+ upgrade << "Upgrade: WebSocket\r\n"
87
+ upgrade << "Connection: Upgrade\r\n"
88
+ upgrade << "WebSocket-Origin: #{headers['Origin']}\r\n"
89
+ upgrade << "WebSocket-Location: #{location}\r\n\r\n"
90
+
91
+ send_data upgrade
92
+ end
93
+
94
+ def setup_connection
95
+ return unless @data =~ /\r\n\r\n$/
96
+
97
+ if valid_connection?
98
+ @data = ''
99
+ @connected = true
100
+
101
+ send_handshake
102
+ open
103
+ else
104
+ send_data "HTTP/1.1 400 Bad Request\r\n\r\n"
105
+ close_connection_after_writing
106
+ end
107
+ end
108
+
109
+ def valid_connection?
110
+ headers['Connection'] == 'Upgrade' &&
111
+ headers['Upgrade'] == 'WebSocket'
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,23 @@
1
+ module EventMachine::WebSocket
2
+ def self.start(options = {}, &block)
3
+ host = options.delete(:host) || '0.0.0.0'
4
+ port = options.delete(:port) || 8080
5
+ klass = options.delete(:klass) || Connection
6
+
7
+ EM.epoll
8
+ EM.run do
9
+ trap('INT') { stop }
10
+ trap('TERM') { stop }
11
+
12
+ EventMachine.start_server(host, port, klass, options) do |connection|
13
+ if block_given?
14
+ block.call(connection)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.stop
21
+ EventMachine.stop
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'em-http'
4
+
5
+ require 'lib/eventmachine-websocket'
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ describe EventMachine::WebSocket do
4
+ it 'should perform WebSocket handshake' do
5
+ EM.run do
6
+ EventMachine.add_timer(0.1) do
7
+ http = EventMachine::HttpRequest.new('ws://127.0.0.1:8080/').get
8
+ http.callback do
9
+ http.response_header.status.should == 101
10
+ EventMachine.stop
11
+ end
12
+ http.errback do
13
+ EventMachine.stop
14
+ fail
15
+ end
16
+ end
17
+
18
+ EventMachine::WebSocket::start do
19
+ end
20
+ end
21
+ end
22
+
23
+ it 'should only accept WebSocket requests' do
24
+ EM.run do
25
+ EventMachine.add_timer(0.1) do
26
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get
27
+ http.callback do
28
+ http.response_header.status.should == 400
29
+ EventMachine.stop
30
+ end
31
+ http.errback do
32
+ EventMachine.stop
33
+ fail
34
+ end
35
+ end
36
+
37
+ EventMachine::WebSocket.start do
38
+ end
39
+ end
40
+ end
41
+
42
+ it 'should split multiple messages into separate callbacks' do
43
+ EM.run do
44
+ messages = %w[1 2]
45
+ received = []
46
+
47
+ EventMachine.add_timer(0.1) do
48
+ http = EventMachine::HttpRequest.new('ws://127.0.0.1:8080/').get
49
+ http.callback do
50
+ http.response_header.status.should == 101
51
+ http.send messages.first
52
+ http.send messages.last
53
+ end
54
+ http.errback do
55
+ EventMachine.stop
56
+ fail
57
+ end
58
+ end
59
+
60
+ EventMachine::WebSocket.start do |socket|
61
+ socket.on_message do |message|
62
+ message.should == messages[received.length]
63
+
64
+ received << message
65
+
66
+ if received.length == 2
67
+ EventMachine.stop
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ it 'should call close callback when client closes connection' do
75
+ EM.run do
76
+ EventMachine.add_timer(0.1) do
77
+ http = EventMachine::HttpRequest.new('ws://127.0.0.1:8080/').get
78
+ http.callback do
79
+ http.response_header.status.should == 101
80
+ http.close_connection
81
+ end
82
+ http.errback do
83
+ EventMachine.stop
84
+ fail
85
+ end
86
+ end
87
+
88
+ EventMachine::WebSocket.start do |socket|
89
+ socket.on_close do
90
+ EventMachine.stop
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ it 'should allow custom client classes' do
97
+ EM.run do
98
+ EventMachine.add_timer(0.1) do
99
+ http = EventMachine::HttpRequest.new('ws://127.0.0.1:8080/').get
100
+ http.callback do
101
+ http.send 'Hello, world.'
102
+ end
103
+ end
104
+
105
+ class Custom < EventMachine::WebSocket::Connection
106
+ def open
107
+ @open = true
108
+ end
109
+
110
+ def close
111
+ @open.should == true
112
+ end
113
+
114
+ def message(data)
115
+ EventMachine.stop
116
+ end
117
+ end
118
+
119
+ EventMachine::WebSocket.start(:klass => Custom)
120
+ end
121
+ end
122
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventmachine-websocket
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Tristan Dunn
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-06 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: An EventMachine based WebSocket server.
22
+ email: hello@tristandunn.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.markdown
29
+ files:
30
+ - README.markdown
31
+ - Rakefile
32
+ - VERSION
33
+ - eventmachine-websocket.gemspec
34
+ - examples/custom.rb
35
+ - examples/echo.rb
36
+ - lib/eventmachine-websocket.rb
37
+ - lib/eventmachine-websocket/connection.rb
38
+ - lib/eventmachine-websocket/websocket.rb
39
+ - spec/spec_helper.rb
40
+ - spec/websocket_spec.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/tristandunn/eventmachine-websocket
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.6
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: An EventMachine based WebSocket server.
71
+ test_files:
72
+ - spec/spec_helper.rb
73
+ - spec/websocket_spec.rb
74
+ - examples/custom.rb
75
+ - examples/echo.rb