aboutsummaryrefslogtreecommitdiff
path: root/third_party/websockify/tests/echo.rb
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-09-30 10:24:53 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-09-30 10:24:53 -0700
commitb614f2bc5d9fc421565824b1ceb9a3384f26f35f (patch)
tree243c47baa4c6ce4273a5743d79f3c0dbc78789e5 /third_party/websockify/tests/echo.rb
parentc70758e3b49beb016a3d9db7b609c499d55de48b (diff)
parent7eaa78060c34489c7e56193c725641303d520f31 (diff)
Merge branch 'incoming'
Diffstat (limited to 'third_party/websockify/tests/echo.rb')
-rwxr-xr-xthird_party/websockify/tests/echo.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/third_party/websockify/tests/echo.rb b/third_party/websockify/tests/echo.rb
new file mode 100755
index 00000000..ea34db5d
--- /dev/null
+++ b/third_party/websockify/tests/echo.rb
@@ -0,0 +1,62 @@
+#!/usr/bin/env ruby
+
+# A WebSocket server that echos back whatever it receives from the client.
+# Copyright 2011 Joel Martin
+# Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
+
+require 'socket'
+$: << "other"
+$: << "../other"
+require 'websocket'
+
+class WebSocketEcho < WebSocketServer
+
+ # Echo back whatever is received
+ def new_client(client)
+
+ cqueue = []
+ c_pend = 0
+ rlist = [client]
+
+ loop do
+ wlist = []
+
+ if cqueue.length > 0 or c_pend
+ wlist << client
+ end
+
+ ins, outs, excepts = IO.select(rlist, wlist, nil, 1)
+ if excepts.length > 0
+ raise Exception, "Socket exception"
+ end
+
+ if outs.include?(client)
+ # Send queued data to the client
+ c_pend = send_frames(cqueue)
+ cqueue = []
+ end
+
+ if ins.include?(client)
+ # Receive client data, decode it, and send it back
+ frames, closed = recv_frames
+ cqueue += frames
+
+ if closed
+ raise EClose, closed
+ end
+ end
+
+ end # loop
+ end
+end
+
+port = ARGV[0].to_i
+puts "Starting server on port #{port}"
+
+server = WebSocketEcho.new('listen_port' => port, 'verbose' => true)
+server.start
+server.join
+
+puts "Server has been terminated"
+
+# vim: sw=2