Reputation: 20203
in my app the socket
is passed around as this
using bind
and call
socket.on( 'handle', someFunc.bind(socket) );
function someFunc() {
var socket = this;
...
anotherFunc.call( socket, 'abc', ... );
}
I wish to ensure that this
is indeed a socket
- forgetting - using func(...)
instead of func.call(socket,...)
.
function someFunc() {
var socket = this;
assert.ok( /* ensure this is instanceof a socket */ );
...
}
how do I make the assertion?
is there some socket
within io = require('socket.io')
that I can use?
Upvotes: 2
Views: 547
Reputation: 9912
To solve the specific problem in question, it is probably better to check whether this
is not null:
function someFunc() {
var socket = this;
assert.ok(socket);
...
}
The assert will fail for someFunc();
call, and will only pass for someFunc.bind(socket)();
and someFunc.call(socket);
calls (as long as socket
is not null).
Relying on the implementation details, as Bergi suggested, will render your application unusable should the next Node.js version feature a different implementation; and it does not seem to be a part of the public API.
Upvotes: 1
Reputation: 664206
From what I read in the source, it returns a Manager
instance. This check should work:
var io = require('socket.io');
var socket = io.listen(...);
socket instanceof io.Manager; // true
Upvotes: 0