<commit_before>JSON WebSocket is a lightweight wrapper around the [websocket](https://github.com/Worlize/WebSocket-Node) implementation. Event types and objects are automatically translated to and from JSON, and emitted as events when received.

# Installation #

Install JsonWebSocket using Node Package Manager (npm):

    npm install json-websocket

# Usage #

Example client (found in `examples/client.js`)

    var main = require('../lib/main.js');

    var client = main.client({ host: 'localhost', port: 8000, verbose: true });

    client.on('js:connect', function() {
      client.send('greeting', 'Websocket client here');
    });

    client.on('tick', function(serverTime) {
      //Emitted when the server sends a "tick" event
    });

    setTimeout(function() {
      client.disconnect();
    }, 1000);

Example server (found in `examples/server.js`)

    var http = require('http').createServer();
    var main = require('../lib/main.js');

    var server = main.server({ httpServer: http, verbose: true });

    server.on('update', function(clientId, value) {
      //Process "update" event from client here
    });

    server.on('js:connect', function(clientId) {
      server.send(clientId, 'greet', 'Greetings, client #' + clientId);
    });

    server.on('js:disconnect', function(clientId) {
      //Client disconnected
    });

    http.listen(8000);

# API #

## Server ##

To create a server, use `require('json-websocket').server(opts);`.

Options:

* `httpServer`: A [http.Server](http://nodejs.org/api/http.html#http_class_http_server) object that the websocket will bind to
* `path`: The URL path the websocket will listen on in the server. Defaults to `/`
* `verbose`: Any truthy value will enable debug logging to `console.log`
* `protocol`: Name of the websocket protocol to support. Defaults to `'json-websocket'`
* `maxListeners`: Controls the maximum number of listeners that can be attached to the resulting EventEmitter. Use `0` for unlimited. Defaults to `10`

This returns an [Event Emitter](http://nodejs.org/api/events.html#events_class_events_emitter) with the following properties:

###Event: 'js:connect'###
`function (clientId) { }`

Emitted each time a new client successfully connects.

###Event: 'js:disconnect'###
`function (clientId) { }`

Emitted each time a client disconnects.

###Event: 'js:error'###
`function (err) { }`

Emitted on errors.

###Event: \*###
`function (clientId, ...) { }`

All messages received by clients are emitted as events with the "type" of the sent message. 

###server.clients###

An object mapping client identifiers to the underlaying websocket connections.

###server.numClients###

The number of currently connected clients.

###server.isReady(clientId)###

Returns `true` if there is a connected client with the given identifier that is in the ready state and has no currently buffered outgoing data, `false` otherwise.

###server.send(clientId, type, ...)###

Send a message to the given client of the given type. "type" must be a String. Any other arguments provided to this function will be transmitted to the client and attached to the emitted event.

`js:connect`, `js:disconnect`, and `js:error` are Illegal message types.

## Client ##

To create a client, use `require('json-websocket').client(opts);`.

Options:

* `hostname`: The host that the client will attempt to connect to. Defaults to `localhost`
* `port`: The port that the client will attempt to connect to. Defaults to `80`
* `path`: The URL path that the client will attempt to connect to. Defaults to `/`
* `verbose`: Any truthy value will enable debug logging to `console.log`
* `protocol`: Name of the websocket protocol to request. Defaults to `'json-websocket'`
* `maxListeners`: Controls the maximum number of listeners that can be attached to the resulting EventEmitter. Use `0` for unlimited. Defaults to `10`

###Event: 'js:connect'###
`function () { }`

Emitted when the socket successfully connects.

###Event: 'js:disconnect'###
`function () { }`

Emitted when the socket is closed.

###Event: 'js:error'###
`function (err) { }`

Emitted on a connection failure.

###Event: \*###

All messages received by the server are emitted as events with the "type" of the sent message.

###client.send(type, ...)###

###client.disconnect()###

###client.isReady()###

## Browser ##

JSON WebSocket can also be used in the browser with no dependency on the node [websocket](https://github.com/Worlize/WebSocket-Node) module relying instead on the browser's implementation.

In order to do this, you should include `./lib/browser.js` using a CommonJS bundler.

# Testing #

Run tests using Node Package Manager (npm):

    npm test

# License #

Copyright (c) 2013 <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights<commit_msg>More README<commit_after>JSON WebSocket is a lightweight wrapper around the [websocket](https://github.com/Worlize/WebSocket-Node) implementation. Event types and objects are automatically translated to and from JSON, and emitted as events when received.

# Installation #

Install JsonWebSocket using Node Package Manager (npm):

    npm install json-websocket

# Usage #

Example client (found in `examples/client.js`)

    var main = require('../lib/main.js');

    var client = main.client({ host: 'localhost', port: 8000, verbose: true });

    client.on('js:connect', function() {
      client.send('greeting', 'Websocket client here');
    });

    client.on('tick', function(serverTime) {
      //Emitted when the server sends a "tick" event
    });

    setTimeout(function() {
      client.disconnect();
    }, 1000);

Example server (found in `examples/server.js`)

    var http = require('http').createServer();
    var main = require('../lib/main.js');

    var server = main.server({ httpServer: http, verbose: true });

    server.on('update', function(clientId, value) {
      //Process "update" event from client here
    });

    server.on('js:connect', function(clientId) {
      server.send(clientId, 'greet', 'Greetings, client #' + clientId);
    });

    server.on('js:disconnect', function(clientId) {
      //Client disconnected
    });

    http.listen(8000);

# API #

## Server ##

To create a server, use `require('json-websocket').server(opts);`.

Options:

* `httpServer`: A [http.Server](http://nodejs.org/api/http.html#http_class_http_server) object that the websocket will bind to
* `path`: The URL path the websocket will listen on in the server. Defaults to `/`
* `verbose`: Any truthy value will enable debug logging to `console.log`
* `protocol`: Name of the websocket protocol to support. Defaults to `'json-websocket'`
* `maxListeners`: Controls the maximum number of listeners that can be attached to the resulting EventEmitter. Use `0` for unlimited. Defaults to `10`

This returns an [Event Emitter](http://nodejs.org/api/events.html#events_class_events_emitter) with the following properties:

###Event: 'js:connect'###
`function (clientId) { }`

Emitted each time a new client successfully connects.

###Event: 'js:disconnect'###
`function (clientId) { }`

Emitted each time a client disconnects.

###Event: 'js:error'###
`function (err) { }`

Emitted on errors.

###Event: \*###
`function (clientId, ...) { }`

All messages received by clients are emitted as events with the "type" of the sent message. 

###server.clients###

An object mapping client identifiers to the underlaying websocket connections.

###server.numClients###

The number of currently connected clients.

###server.isReady(clientId)###

Returns `true` if there is a connected client with the given identifier that is in the ready state and has no currently buffered outgoing data, `false` otherwise.

###server.send(clientId, type, ...)###

Send a message to the given client of the given type. "type" must be a String. Any other arguments provided to this function will be transmitted to the client and attached to the emitted event.

`js:connect`, `js:disconnect`, and `js:error` are Illegal message types.

## Client ##

To create a client, use `require('json-websocket').client(opts);`.

Options:

* `hostname`: The host that the client will attempt to connect to. Defaults to `localhost`
* `port`: The port that the client will attempt to connect to. Defaults to `80`
* `path`: The URL path that the client will attempt to connect to. Defaults to `/`
* `verbose`: Any truthy value will enable debug logging to `console.log`
* `protocol`: Name of the websocket protocol to request. Defaults to `'json-websocket'`
* `maxListeners`: Controls the maximum number of listeners that can be attached to the resulting EventEmitter. Use `0` for unlimited. Defaults to `10`

###Event: 'js:connect'###
`function () { }`

Emitted when the socket successfully connects.

###Event: 'js:disconnect'###
`function () { }`

Emitted when the socket is closed.

###Event: 'js:error'###
`function (err) { }`

Emitted on a connection failure.

###Event: \*###

All messages received by the server are emitted as events with the "type" of the sent message.

###client.send(type, ...)###

Send a message to the server with the given type. Type must be a `String` and cannot be `js:connect`, `js:disconnect`, or `js:error`. Any additional arguments will be sent along to the server and attached to the emitted event.

Messages sent before the `js:connect` event has been emitted will be queued and sent once the connection is established.

###client.disconnect()###

Closes the websocket connection.

###client.isReady()###

Returns `true` if the websocket connection is established, ready, and has no buffered outgoing data, `false` otherwise.

## Browser ##

JSON WebSocket can also be used in the browser with no dependency on the node [websocket](https://github.com/Worlize/WebSocket-Node) module relying instead on the browser's implementation.

In order to do this, you should include `./lib/browser.js` using a CommonJS bundler.

# Testing #

Run tests using Node Package Manager (npm):

    npm test

# License #

Copyright (c) 2013 <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
{'fraction_non_alphanumeric': 0.1222126519284142, 'fraction_numerical': 0.004593741027849555, 'mean_word_length': 4.320773930753564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3885582', 'n_tokens_mistral': 3068, 'n_tokens_neox': 2905, 'n_words': 1209}