Skip to content

Commit

Permalink
add init project files
Browse files Browse the repository at this point in the history
  • Loading branch information
inosin committed Apr 10, 2013
1 parent 4e7cbdb commit 550e0a4
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

#gem "em-websocket"
gem "em-websocket", "0.3.8"
gem "goliath"
39 changes: 39 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.3.3)
async-rack (0.5.1)
rack (~> 1.1)
em-synchrony (1.0.3)
eventmachine (>= 1.0.0.beta.1)
em-websocket (0.3.8)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
eventmachine (1.0.3)
goliath (1.0.1)
async-rack
em-synchrony (>= 1.0.0)
em-websocket
eventmachine (>= 1.0.0.beta.4)
http_parser.rb (= 0.5.3)
log4r
multi_json
rack (>= 1.2.2)
rack-contrib
rack-respond_to
http_parser.rb (0.5.3)
log4r (1.1.10)
multi_json (1.7.2)
rack (1.5.2)
rack-accept-media-types (0.9)
rack-contrib (1.1.0)
rack (>= 0.9.1)
rack-respond_to (0.9.8)
rack-accept-media-types (>= 0.6)

PLATFORMS
ruby

DEPENDENCIES
em-websocket (= 0.3.8)
goliath
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
ruby-websocket
==============
#ruby-websocket
ruby websocket connection test

ruby websocket connection test
###with em-websocket 0.5.0
bundle exec ruby websocket.rb

###with em-websocket 0.3.8
bundle exec ruby websocket_old.rb
bundle exec ruby goliath_websocket.rb -e prod -sv -p 8080
6 changes: 6 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if ("WebSocket" in window) {
var ws = new WebSocket("ws://localhost:8080/ws");
ws.onmessage = function(evt) { };
ws.onclose = function() { };
ws.onopen = function() { };
}
1 change: 1 addition & 0 deletions config/goliath_websocket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config['channel'] = EM::Channel.new
27 changes: 27 additions & 0 deletions goliath_websocket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby

require 'goliath'
require 'goliath/websocket'

class Websocket < Goliath::WebSocket

def on_open(env)
env.logger.info("WS OPEN")
end

def on_message(env, msg)
env.logger.info("WS MESSAGE: #{msg}")
end

def on_close(env)
env.logger.info("WS CLOSED")
end

def on_error(env, error)
env.logger.error error
end

def response(env)
super(env)
end
end
29 changes: 29 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<head>
<script>
function init() {
function debug(string) {
var element = document.getElementById("debug");
var p = document.createElement("p");
p.appendChild(document.createTextNode(string));
element.appendChild(p);
}

var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
var ws = new Socket("ws://localhost:8080/foo/bar?hello=world");
ws.onmessage = function(evt) { debug("Received: " + evt.data); };
ws.onclose = function(event) {
debug("Closed - code: " + event.code + ", reason: " + event.reason + ", wasClean: " + event.wasClean);
};
ws.onopen = function() {
debug("connected...");
ws.send("hello server");
ws.send("hello again");
};
};
</script>
</head>
<body onload="init();">
<div id="debug"></div>
</body>
</html>
16 changes: 16 additions & 0 deletions websocket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'em-websocket'

# only em-websocket 0.5.0
EM.run {
EM::WebSocket.run(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws|
ws.onopen { |handshake|puts "WebSocket opened #{{
:path => handshake.path,
:query => handshake.query,
:origin => handshake.origin,
}}"
}
ws.onmessage { |msg| ws.send "Pong: #{msg}" }
ws.onclose { puts "WebSocket closed" }
ws.onerror { |e| puts "Error: #{e.message}" }
end
}
11 changes: 11 additions & 0 deletions websocket_old.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'em-websocket'

# em-websocket 0.3.8 & 0.5.0
EventMachine.run do
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen { |handshake| puts "WebSocket opened" }
ws.onmessage { |msg| ws.send "Pong: #{msg}" }
ws.onclose { puts "WebSocket closed" }
ws.onerror { |e| puts "Error: #{e.message}" }
end
end

0 comments on commit 550e0a4

Please sign in to comment.