Skip to content

Commit 3b92cc2

Browse files
[feature] Allow the use of custom parsers (#2829)
1 parent 3d695c6 commit 3b92cc2

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

docs/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Exposed by `require('socket.io')`.
6868
- `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)
6969
- `origins` _(String)_: the allowed origins (`*`)
7070
- `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.
71+
- `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).
7172

7273
Works with and without `new`:
7374

lib/client.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ var url = require('url');
1313

1414
module.exports = Client;
1515

16-
/**
17-
* Packet encoder
18-
*/
19-
20-
var encoder = new parser.Encoder();
21-
2216
/**
2317
* Client constructor.
2418
*
@@ -30,7 +24,8 @@ var encoder = new parser.Encoder();
3024
function Client(server, conn){
3125
this.server = server;
3226
this.conn = conn;
33-
this.decoder = new parser.Decoder();
27+
this.encoder = server.encoder;
28+
this.decoder = new server.parser.Decoder();
3429
this.id = conn.id;
3530
this.request = conn.request;
3631
this.setup();
@@ -158,7 +153,7 @@ Client.prototype.packet = function(packet, opts){
158153
if ('open' == this.conn.readyState) {
159154
debug('writing packet %j', packet);
160155
if (!opts.preEncoded) { // not broadcasting, need to encode
161-
encoder.encode(packet, writeToEngine); // encode, then write results to engine
156+
this.encoder.encode(packet, writeToEngine); // encode, then write results to engine
162157
} else { // a broadcast pre-encodes a packet
163158
writeToEngine(packet);
164159
}

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var Client = require('./client');
1414
var Emitter = require('events').EventEmitter;
1515
var Namespace = require('./namespace');
1616
var Adapter = require('socket.io-adapter');
17+
var parser = require('socket.io-parser');
1718
var debug = require('debug')('socket.io:server');
1819
var url = require('url');
1920

@@ -51,6 +52,8 @@ function Server(srv, opts){
5152
this.adapter(opts.adapter || Adapter);
5253
this.origins(opts.origins || '*:*');
5354
this.sockets = this.of('/');
55+
this.parser = opts.parser || parser;
56+
this.encoder = new this.parser.Encoder();
5457
if (srv) this.attach(srv, opts);
5558
}
5659

0 commit comments

Comments
 (0)