From 01b97eeae2f48e23ba8fe299e2b05bf7076a996d Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 19 Jan 2017 15:17:53 +0100 Subject: [PATCH] Allow the use of a custom parser --- docs/API.md | 1 + lib/client.js | 11 +++-------- lib/index.js | 3 +++ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/docs/API.md b/docs/API.md index f870637808..97b40d96c8 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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`: diff --git a/lib/client.js b/lib/client.js index e5ab6c6626..0b5f0446e9 100644 --- a/lib/client.js +++ b/lib/client.js @@ -13,12 +13,6 @@ var url = require('url'); module.exports = Client; -/** - * Packet encoder - */ - -var encoder = new parser.Encoder(); - /** * Client constructor. * @@ -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(); @@ -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); } diff --git a/lib/index.js b/lib/index.js index 16bc04bbb0..f62dabd090 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,6 +12,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'); @@ -49,6 +50,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); }