Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 101 lines (92 sloc) 3.203 kB
7fdc54a @davide HTML5 Web Sockets support.
davide authored
1 <erl>
2
785b7c6 @davide Added documentation for the HTML5 Web Sockets implementation.
davide authored
3 out(A) ->
7fdc54a @davide HTML5 Web Sockets support.
davide authored
4 Host = (A#arg.headers)#headers.host,
5 {abs_path, Path} = (A#arg.req)#http_request.path,
785b7c6 @davide Added documentation for the HTML5 Web Sockets implementation.
davide authored
6 EndpointPath = filename:dirname(Path)
7 ++ "websockets_example_endpoint.yaws",
8 WebSocketLocation = Host ++ EndpointPath,
7fdc54a @davide HTML5 Web Sockets support.
davide authored
9 Body = html_body(WebSocketLocation),
10 {content, "text/html", Body}.
11
12 %% this html was copied from the basic example in
13 %% http://github.com/davebryson/erlang_websocket/
14 html_body(WebSocketLocation) ->
15 "<html>
7811247 @vinoski whitespace cleanup
vinoski authored
16 <head>
17 <title>Basic WebSocket Example</title>
18 <script type=\"text/javascript\">
39c8d49 @jbothma update WebSockets implementation to support RFC 6455
jbothma authored
19 var WS = false;
20 if (window.WebSocket) WS = WebSocket;
21 if (!WS && window.MozWebSocket) WS = MozWebSocket;
22 if (!WS)
7fdc54a @davide HTML5 Web Sockets support.
davide authored
23 alert(\"WebSocket not supported by this browser\");
7811247 @vinoski whitespace cleanup
vinoski authored
24
7fdc54a @davide HTML5 Web Sockets support.
davide authored
25 // Get an Element
26 function $() { return document.getElementById(arguments[0]); }
27 // Get the value of an Element
28 function $F() { return document.getElementById(arguments[0]).value; }
7811247 @vinoski whitespace cleanup
vinoski authored
29
7fdc54a @davide HTML5 Web Sockets support.
davide authored
30 var client = {
31 connect: function(){
39c8d49 @jbothma update WebSockets implementation to support RFC 6455
jbothma authored
32 this._ws=new WS(\"ws://" ++ WebSocketLocation ++ "\");
7fdc54a @davide HTML5 Web Sockets support.
davide authored
33 this._ws.onopen=this._onopen;
34 this._ws.onmessage=this._onmessage;
35 this._ws.onclose=this._onclose;
36 },
37 _onopen: function(){
38 $('connect').className='hidden';
39 $('connected').className='';
40 $('phrase').focus();
41 client._send('client-connected');
42 },
43 _send: function(message){
44 if (this._ws)
45 this._ws.send(message);
46 },
47 chat: function(text) {
48 if (text != null && text.length>0 )
49 client._send(text);
50 },
51 _onmessage: function(m) {
52 if (m.data){
7811247 @vinoski whitespace cleanup
vinoski authored
53 var text = m.data;
7fdc54a @davide HTML5 Web Sockets support.
davide authored
54 var msg=$('msgs');
55 var spanText = document.createElement('span');
56 spanText.className='text';
57 spanText.innerHTML=text;
58 var lineBreak = document.createElement('br');
59 msg.appendChild(spanText);
60 msg.appendChild(lineBreak);
7811247 @vinoski whitespace cleanup
vinoski authored
61 msg.scrollTop = msg.scrollHeight - msg.clientHeight;
7fdc54a @davide HTML5 Web Sockets support.
davide authored
62 }
63 },
64 _onclose: function(m) {
65 this._ws=null;
66 $('connect').className='';
67 $('connected').className='hidden';
68 $('msg').innerHTML='';
69 }
70 };
7811247 @vinoski whitespace cleanup
vinoski authored
71 </script>
72 <style type='text/css'>
7fdc54a @davide HTML5 Web Sockets support.
davide authored
73 div.hidden { display: none; }
7811247 @vinoski whitespace cleanup
vinoski authored
74 </style>
75
76 </head>
77 <body>
78 <h1>Basic Echo Example</h1>
39c8d49 @jbothma update WebSockets implementation to support RFC 6455
jbothma authored
79 <ul><li>Sending \"bye\" closes the connection.</li>
80 <li>Sending \"say hi later\" sends \"hi there!\" asynchronously.</li>
81 <li>Sending \"something\" does nothing.</li></ul>
7811247 @vinoski whitespace cleanup
vinoski authored
82 <div id=\"msgs\"></div>
83 <div id=\"connect\">
84 <input id='cA' class='button' type='submit' name='connect' value='Connect'/>
85 </div>
86 <br/>
87 <div id=\"connected\" class=\"hidden\">
88 Say Something:&nbsp;<input id='phrase' type='text'/>
89 <input id='sendB' class='button' type='submit' name='connect' value='Send'/>
90 </div>
91
92 <script type='text/javascript'>
7fdc54a @davide HTML5 Web Sockets support.
davide authored
93 $('cA').onclick = function(event) { client.connect(); return false; };
39c8d49 @jbothma update WebSockets implementation to support RFC 6455
jbothma authored
94 $('sendB').onclick = function(event) { client.chat($F('phrase')); $('phrase').value=''; $('phrase').focus(); return false; };
7811247 @vinoski whitespace cleanup
vinoski authored
95 </script>
96 </body>
7fdc54a @davide HTML5 Web Sockets support.
davide authored
97 </html>".
98
99 </erl>
100
Something went wrong with that request. Please try again.