Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 95 lines (71 sloc) 2.928 kb
1d8000a @einaros initial import
einaros authored
1 # easy-websocket #
2
8aee35a @einaros typo fixo
einaros authored
3 `easy-websocket` aims to be an easy to use websocket client for node.js, up-to-date against current HyBi protocol versions.
1d8000a @einaros initial import
einaros authored
4
52a4486 @einaros cleanups
einaros authored
5 ## Usage ##
fc00417 @einaros npm reference
einaros authored
6
52a4486 @einaros cleanups
einaros authored
7 ### Installing ###
fc00417 @einaros npm reference
einaros authored
8
52a4486 @einaros cleanups
einaros authored
9 `npm install easy-websocket`
1d8000a @einaros initial import
einaros authored
10
11 ### Sending and receiving text data ###
12
13 ```js
d13a0bd @einaros added require example
einaros authored
14 var WebSocket = require('easy-websocket');
1d8000a @einaros initial import
einaros authored
15 var ws = new WebSocket('ws://www.host.com/path');
1da4975 @einaros updated examples to fit 0.1.2 api
einaros authored
16 ws.on('open', function() {
1d8000a @einaros initial import
einaros authored
17 ws.send('something');
18 });
1da4975 @einaros updated examples to fit 0.1.2 api
einaros authored
19 ws.on('message', function(data, flags) {
9f1df5d @einaros message renamed to data
einaros authored
20 // flags.binary will be set if a binary data is received
21 // flags.masked will be set if the data was masked
1d8000a @einaros initial import
einaros authored
22 });
23 ```
24
25 ### Sending binary data ###
26
27 ```js
d13a0bd @einaros added require example
einaros authored
28 var WebSocket = require('easy-websocket');
1d8000a @einaros initial import
einaros authored
29 var ws = new WebSocket('ws://www.host.com/path');
1da4975 @einaros updated examples to fit 0.1.2 api
einaros authored
30 ws.on('open', function() {
1d8000a @einaros initial import
einaros authored
31 var array = new Float32Array(5);
32 for (var i = 0; i < array.length; ++i) array[i] = i / 2;
33 ws.send(array, {binary: true, mask: true});
34 });
35 ```
36
9f1df5d @einaros message renamed to data
einaros authored
37 Setting `mask`, as done for the send options above, will cause the data to be masked according to the websocket protocol. The same option applies for text data.
1d8000a @einaros initial import
einaros authored
38
a970f1b @einaros echo.websocket.org demo
einaros authored
39 ### echo.websocket.org demo ###
40
41 ```js
42 var WebSocket = require('easy-websocket');
43 var ws = new WebSocket('ws://echo.websocket.org/', {protocolVersion: 8, origin: 'http://websocket.org'});
1da4975 @einaros updated examples to fit 0.1.2 api
einaros authored
44 ws.on('open', function() {
a970f1b @einaros echo.websocket.org demo
einaros authored
45 console.log('connected');
46 ws.send(Date.now().toString(), {mask: true});
47 });
1da4975 @einaros updated examples to fit 0.1.2 api
einaros authored
48 ws.on('close', function() {
a970f1b @einaros echo.websocket.org demo
einaros authored
49 console.log('disconnected');
50 });
1da4975 @einaros updated examples to fit 0.1.2 api
einaros authored
51 ws.on('message', function(data, flags) {
a970f1b @einaros echo.websocket.org demo
einaros authored
52 console.log('Roundtrip time: ' + (Date.now() - parseInt(data)) + 'ms', flags);
53 setTimeout(function() {
54 ws.send(Date.now().toString(), {mask: true});
55 }, 500);
56 });
57 ```
58
1d8000a @einaros initial import
einaros authored
59 ### Other examples ###
60
61 See the test cases.
62
71dedfa @einaros added test running info
einaros authored
63 ### Running the tests ###
64
65 `make test`
66
1d8000a @einaros initial import
einaros authored
67 ## Yet to be done ##
68
e0a0c3a @einaros sequence tested and implemented for long running sends. bump.
einaros authored
69 Nothing planned, but certainly something is in the works.
1d8000a @einaros initial import
einaros authored
70
71 ## License ##
72
73 (The MIT License)
74
75 Copyright (c) 2011 Einar Otto Stangvik &lt;einaros@gmail.com&gt;
76
77 Permission is hereby granted, free of charge, to any person obtaining
78 a copy of this software and associated documentation files (the
79 'Software'), to deal in the Software without restriction, including
80 without limitation the rights to use, copy, modify, merge, publish,
81 distribute, sublicense, and/or sell copies of the Software, and to
82 permit persons to whom the Software is furnished to do so, subject to
83 the following conditions:
84
85 The above copyright notice and this permission notice shall be
86 included in all copies or substantial portions of the Software.
87
88 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
89 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
90 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
91 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
92 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
93 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
a970f1b @einaros echo.websocket.org demo
einaros authored
94 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.