Skip to content

Commit

Permalink
[feature] Allow the use of custom parsers (#2829)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 24, 2017
1 parent 3d695c6 commit 3b92cc2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/API.md
Expand Up @@ -68,6 +68,7 @@ Exposed by `require('socket.io')`.
- `adapter` _(Adapter)_: the adapter to use. Defaults to an instance of the `Adapter` that ships with socket.io which is memory based. See [socket.io-adapter](https://github.com/socketio/socket.io-adapter)
- `origins` _(String)_: the allowed origins (`*`)
- `allowRequest` _(Function)_: A function that receives a given handshake or upgrade request as its first parameter, and can decide whether to continue or not. The second argument is a function that needs to be called with the decided information: `fn(err, success)`, where `success` is a boolean value where false means that the request is rejected, and err is an error code.
- `parser` _(Parser)_: the parser to use. Defaults to an instance of the `Parser` that ships with socket.io. See [socket.io-parser](https://github.com/socketio/socket.io-parser).

Works with and without `new`:

Expand Down
11 changes: 3 additions & 8 deletions lib/client.js
Expand Up @@ -13,12 +13,6 @@ var url = require('url');

module.exports = Client;

/**
* Packet encoder
*/

var encoder = new parser.Encoder();

/**
* Client constructor.
*
Expand All @@ -30,7 +24,8 @@ var encoder = new parser.Encoder();
function Client(server, conn){
this.server = server;
this.conn = conn;
this.decoder = new parser.Decoder();
this.encoder = server.encoder;
this.decoder = new server.parser.Decoder();
this.id = conn.id;
this.request = conn.request;
this.setup();
Expand Down Expand Up @@ -158,7 +153,7 @@ Client.prototype.packet = function(packet, opts){
if ('open' == this.conn.readyState) {
debug('writing packet %j', packet);
if (!opts.preEncoded) { // not broadcasting, need to encode
encoder.encode(packet, writeToEngine); // encode, then write results to engine
this.encoder.encode(packet, writeToEngine); // encode, then write results to engine
} else { // a broadcast pre-encodes a packet
writeToEngine(packet);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Expand Up @@ -14,6 +14,7 @@ var Client = require('./client');
var Emitter = require('events').EventEmitter;
var Namespace = require('./namespace');
var Adapter = require('socket.io-adapter');
var parser = require('socket.io-parser');
var debug = require('debug')('socket.io:server');
var url = require('url');

Expand Down Expand Up @@ -51,6 +52,8 @@ function Server(srv, opts){
this.adapter(opts.adapter || Adapter);
this.origins(opts.origins || '*:*');
this.sockets = this.of('/');
this.parser = opts.parser || parser;
this.encoder = new this.parser.Encoder();
if (srv) this.attach(srv, opts);
}

Expand Down

0 comments on commit 3b92cc2

Please sign in to comment.