Skip to content

Commit 83208b6

Browse files
committedDec 25, 2013
socket: add conn, client, request getters
1 parent 036faa7 commit 83208b6

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed
 

Diff for: ‎Readme.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,65 @@ server.listen(3000);
202202
Hash of `Socket` objects that are connected to this namespace indexed
203203
by `id`.
204204
205+
### Namespace#use(fn:Function):Namespace
206+
207+
Registers a middleware, which is a function that gets executed for
208+
every incoming `Socket` and receives as parameter the socket and a
209+
function to optionally defer execution to the next registered
210+
middleware.
211+
212+
```
213+
var io = require('socket.io')();
214+
io.use(function(socket, next){
215+
if (socket.request.headers.cookie) return next();
216+
done(new Error('Authentication error'));
217+
});
218+
```
219+
220+
Errors passed to middleware callbacks are sent as special `error`
221+
packets to clients.
222+
205223
### Socket
206224
207225
A `Socket` is the fundamental class for interacting with browser
208-
clients.
226+
clients. A `Socket` belongs to a certain `Namespace` (by default `/`)
227+
and uses an underlying `Client` to communicate.
209228
210229
### Socket#rooms:Array
211230
212231
A list of strings identifying the rooms this socket is in.
213232
233+
### Socket#client:Client
234+
235+
A reference to the underlying `Client` object.
236+
237+
### Socket#conn:Socket
238+
239+
A reference to the underyling `Client` transport connection (engine.io
240+
`Socket` object).
241+
242+
### Socket#request:Request
243+
244+
A getter proxy that returns the reference to the `request` that
245+
originated the underlying engine.io `Client`. Useful for accessing
246+
request headers such as `Cookie` or `User-Agent`.
247+
214248
### Client
215249
216250
The `Client` class represents an incoming transport (engine.io)
217251
connection. A `Client` can be associated with many multiplexed `Socket`
218252
that belong to different `Namespace`s.
219253
254+
### Client#conn
255+
256+
A reference to the underlying `engine.io` `Socket` connection.
257+
258+
### Client#request
259+
260+
A getter proxy that returns the reference to the `request` that
261+
originated the engine.io connection. Useful for accessing
262+
request headers such as `Cookie` or `User-Agent`.
263+
220264
### Adapter
221265
222266
The `Adapter` is in charge of keeping track of what rooms each socket

Diff for: ‎lib/socket.js

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function Socket(nsp, client){
5959
this.id = client.id;
6060
this.request = client.request;
6161
this.client = client;
62+
this.conn = client.conn;
6263
this.rooms = [];
6364
this.acks = {};
6465
this.connected = true;
@@ -83,6 +84,16 @@ exports.flags.forEach(function(flag){
8384
});
8485
});
8586

87+
/**
88+
* `request` engine.io shorcut.
89+
*
90+
* @api public
91+
*/
92+
93+
Socket.prototype.__defineGetter__('request', function(){
94+
return this.conn.request;
95+
});
96+
8697
/**
8798
* Emits to this client.
8899
*

Diff for: ‎test/socket.io.js

+38
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,44 @@ describe('socket.io', function(){
347347
});
348348
});
349349
});
350+
351+
it('should have access to the client', function(done){
352+
var srv = http();
353+
var sio = io(srv);
354+
srv.listen(function(){
355+
var socket = client(srv);
356+
sio.on('connection', function(s){
357+
expect(s.client).to.be.an('object');
358+
done();
359+
});
360+
});
361+
});
362+
363+
it('should have access to the connection', function(done){
364+
var srv = http();
365+
var sio = io(srv);
366+
srv.listen(function(){
367+
var socket = client(srv);
368+
sio.on('connection', function(s){
369+
expect(s.client.conn).to.be.an('object');
370+
expect(s.conn).to.be.an('object');
371+
done();
372+
});
373+
});
374+
});
375+
376+
it('should have access to the request', function(done){
377+
var srv = http();
378+
var sio = io(srv);
379+
srv.listen(function(){
380+
var socket = client(srv);
381+
sio.on('connection', function(s){
382+
expect(s.client.request.headers).to.be.an('object');
383+
expect(s.request.headers).to.be.an('object');
384+
done();
385+
});
386+
});
387+
});
350388
});
351389

352390
describe('messaging many', function(){

0 commit comments

Comments
 (0)
Please sign in to comment.