|
1 | | -jsonet |
| 1 | +JavaScript Object Networking (jsonet) |
2 | 2 | ====== |
3 | 3 | [](https://travis-ci.org/dotdecimal/jsonet) |
4 | 4 | [](https://coveralls.io/r/dotdecimal/jsonet) |
5 | 5 |
|
6 | 6 | JavaScript Object Network messaging server/client for Node.js |
| 7 | + |
| 8 | +This library provides a server and a socket class (with APIs that are very similar to the net package in Node.js) which communicate by sending each other JSON messages. |
| 9 | + |
| 10 | +You "write" JSON objects to the socket, and the "message" events on the other end of the socket emits the JSON object you wrote. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | + $ npm install jsonet |
| 15 | + |
| 16 | +## Example |
| 17 | + |
| 18 | +``` javascript |
| 19 | +var port = 8212; |
| 20 | + |
| 21 | +// Create server |
| 22 | +var server = jsonet.createServer(function (socket) { |
| 23 | + |
| 24 | + // Write messages received by server to console |
| 25 | + socket.on('message', function (message) { |
| 26 | + console.log(message); |
| 27 | + socket.write({ |
| 28 | + boo: 'baz' |
| 29 | + }); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +// Listen to port |
| 34 | +server.listen(port, function () { |
| 35 | + |
| 36 | + // Connect to server with new client socket |
| 37 | + var client = jsonet.createSocket(); |
| 38 | + |
| 39 | + // Write messages received by client to console |
| 40 | + client.on('message', function (message) { |
| 41 | + console.log(message); |
| 42 | + }); |
| 43 | + |
| 44 | + // Connect and write message to server |
| 45 | + client.connect(port, function () { |
| 46 | + client.write({ |
| 47 | + foo: 'bar' |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +## Factories |
| 54 | + |
| 55 | +### jsonet.createServer([options], [connectionListener]) |
| 56 | +Creates a new TCP server. The ```connectionListener``` argument is automatically set as a listener for the 'connection' event. |
| 57 | + |
| 58 | +See Node.js documentation for [net.createServer([options], [connectionListener])](http://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener) for more details. |
| 59 | + |
| 60 | +**TODO: Complete remaining Factory documentation** |
| 61 | + |
| 62 | +## Server |
| 63 | + |
| 64 | +### server.listen(port, [host], [backlog], [callback]) |
| 65 | +Begin accepting connections on the specified ```port``` and ```host```. If the ```host``` is omitted, the server will accept connections directed to any IPv4 address (```INADDR_ANY```). A port value of zero will assign a random port. |
| 66 | + |
| 67 | +Backlog is the maximum length of the queue of pending connections. The actual length will be determined by your OS through sysctl settings such as ```tcp_max_syn_backlog``` and ```somaxconn``` on linux. The default value of this parameter is 511 (not 512). |
| 68 | + |
| 69 | +This function is asynchronous. When the server has been bound, 'listening' event will be emitted. The last parameter callback will be added as an listener for the 'listening' event. |
| 70 | + |
| 71 | +One issue some users run into is getting ```EADDRINUSE``` errors. This means that another server is already running on the requested port. One way of handling this would be to wait a second and then try again. |
| 72 | + |
| 73 | +See Node.js documentation for [server.listen(port, [host], [backlog], [callback])](http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback) for more details. |
| 74 | + |
| 75 | +**TODO: Complete remaining Server documentation** |
| 76 | + |
| 77 | +## Socket |
| 78 | + |
| 79 | +**TODO: Complete Socket documentation** |
| 80 | + |
| 81 | +## Protocol |
| 82 | + |
| 83 | +If you would like to implement the protocol yourself, the server will expect the following in order in the byte stream: |
| 84 | + |
| 85 | +1. A 32-bit unsigned big-endian integer with 174021652 as the value. This is the protocol signature, if a message is sent without this signature a protocol error will be raised. |
| 86 | +2. A 32-bit unsigned big-endian integer with the length of the message being sent as the value. |
| 87 | +3. A UTF-8 string with the stringified JSON as the value (the message). |
0 commit comments