Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 94 lines (76 sloc) 2.061 kb
4098f1c @rubys Convert chat demo to use ruby2js
authored
1 # Instructions: install em-websocket parser gems. Then set this script up to
2 # run as CGI. In a separate window, run this same script from the command
3 # line. Point multiple browsers at the CGI window, and change the textarea
4 # from in each.
d193240 @rubys Bidrectional WebSocket support
authored
5
b279337 @rubys Pull in web sockets
authored
6 require 'wunderbar/jquery'
7 require 'wunderbar/websocket'
d193240 @rubys Bidrectional WebSocket support
authored
8
4098f1c @rubys Convert chat demo to use ruby2js
authored
9 PORT = 8080
d193240 @rubys Bidrectional WebSocket support
authored
10
11 if ENV['SERVER_PORT']
12
13 _html do
4098f1c @rubys Convert chat demo to use ruby2js
authored
14 _style_ %{
15 textarea {width: 100%; height: 10em}
16 #error {color: red; margin-top: 1em}
17 #error pre {margin: 0}
18 }
19
20 _h1_ "Chat on port # #{PORT}"
21 _textarea
22 _div.status!
23 _div.error!
24
25 @socket = "ws://#{env['HTTP_HOST']}:#{PORT}/"
26
27 _script_ do
28 ws = WebSocket.new(@socket)
29 ~'textarea'.on(:input) { ws.send(~this.val) }
b279337 @rubys Pull in web sockets
authored
30
4098f1c @rubys Convert chat demo to use ruby2js
authored
31 ws.onmessage = proc do |evt|
32 data = JSON.parse(evt.data)
33
34 case data.type
35 when 'status'
36 ~'#status'.text = data.line
37 when 'stderr'
38 ~"#error".append(~'<pre>').text = data.line
39 else
40 ~'textarea'.val = data.line
41 end
42 end
43
44 ws.onclose = proc do
45 ~'textarea'.readonly = true
46 ~'#status'.text = 'chat terminated'
47 end
d193240 @rubys Bidrectional WebSocket support
authored
48 end
49 end
50
51 else
52
53 # echo server
4098f1c @rubys Convert chat demo to use ruby2js
authored
54 puts "Waiting on port #{PORT}"
55 _websocket(port: PORT) do
98bfa6e @rubys add onopen, onmessage, onerror, and onclose to websocket
authored
56 count = 0
57 timer = 10
4098f1c @rubys Convert chat demo to use ruby2js
authored
58 content = ''
98bfa6e @rubys add onopen, onmessage, onerror, and onclose to websocket
authored
59
60 _.onopen do
61 count += 1
62 _ type: 'status', line: "waiting for others to join" if count == 1
63 _ type: 'status', line: "#{count} members in chat" if count > 1
4098f1c @rubys Convert chat demo to use ruby2js
authored
64 _ type: 'msg', line: content
98bfa6e @rubys add onopen, onmessage, onerror, and onclose to websocket
authored
65 end
66
d193240 @rubys Bidrectional WebSocket support
authored
67 _.subscribe do |msg|
68 puts msg
98bfa6e @rubys add onopen, onmessage, onerror, and onclose to websocket
authored
69 _ type: 'msg', line: msg
4098f1c @rubys Convert chat demo to use ruby2js
authored
70 content = msg
98bfa6e @rubys add onopen, onmessage, onerror, and onclose to websocket
authored
71 end
72
73 _.onclose do
74 count -= 1
75 _ type: 'status', line: "waiting for others to join" if count == 1
76 _ type: 'status', line: "#{count} members in chat" if count > 1
77 end
78
79 loop do
d012d15 @rubys Make demos more like real applications
authored
80 begin
81 sleep 60
82 timer -= 1
83 timer = 10 if count > 0
84 break if timer <= 0
85 rescue Interrupt
86 puts 'Shutting down'
87 _ type: 'status', line: 'shutting down'
88 break
89 end
d193240 @rubys Bidrectional WebSocket support
authored
90 end
91 end
92
93 end
Something went wrong with that request. Please try again.