Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of a custom parser #2829

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
}

Expand Down