Skip to content

Commit 45711cd

Browse files
committed
WIP logs
1 parent 7e73a49 commit 45711cd

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

README.md

+24-5
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ If Echo is behind a proxy will get the client's Ip from X-forward headers.
107107
* true
108108
* false
109109

110+
## Tests
111+
```sh
112+
$> npm run build && npm run test
113+
114+
Laravel Echo Auth Methods
115+
✓ should Fail Authenticate with Bearer Token Header and should be disconnected (98ms)
116+
✓ should Authenticate Correctly with Bearer Token Header
117+
✓ should Fail Authenticate with Query Token
118+
✓ should Authenticate Correctly with Query Token
119+
✓ should Fail Authenticate with Cookie Token
120+
✓ should Authenticate Correctly with Cookie Token
121+
122+
6 passing (280ms)
123+
124+
Laravel Echo Multiple Sockets not Allowed
125+
✓ Second Connection to Echo Server is not allowed with multiple_sockets = false (3024ms)
126+
127+
Laravel Echo Multiple Sockets Allowed
128+
✓ Second Connection to Echo Server is allowed with multiple_sockets = true (3011ms)
129+
130+
2 passing (6s)
131+
```
132+
133+
110134
# Laravel backend
111135
##### routes/channels.php
112136
```php
@@ -218,11 +242,6 @@ template(name="bunyan" type="string"
218242
node_modules/bunyan/bin/bunyan /var/log/myapp.log
219243
```
220244

221-
## Tests
222-
```sh
223-
$> npm run build && npm run test
224-
```
225-
226245
### Laravel Echo Auth and Message Flow
227246
![Auth Flow, Message Flow](laravelEcho.png)
228247

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"build": "tsc",
2121
"dev": "tsc -w",
2222
"prepublish": "npm run build",
23-
"test": "node_modules/.bin/_mocha --exit dist/test/auth/index.js"
23+
"test": "node_modules/.bin/_mocha --exit dist/test/auth/index.js && node_modules/.bin/_mocha --exit dist/test/multiple_sockets/index.js"
2424
},
2525
"dependencies": {
2626
"bunyan": "^1.8.12",

src/channels/baseAuthChannel.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export class BaseAuthChannel {
6565

6666
/**
6767
* Send a request to the server.
68+
*
69+
* { channel_data: { user_id: 2, user_info: 2 } }
6870
*/
6971
protected serverRequest(socket: any, options: any): Promise<any> {
7072
return new Promise<any>((resolve, reject) => {
@@ -94,15 +96,21 @@ export class BaseAuthChannel {
9496
});
9597

9698
} else {
97-
Log.info(`[${new Date().toLocaleTimeString()}] - ${socket.id} authenticated for: ${options.form.channel_name}`);
98-
this.log.info(`${socket.id} authenticated for: ${options.form.channel_name}`);
99-
10099
try {
101100
body = JSON.parse(response.body);
102101
} catch (e) {
103102
body = response.body
104103
}
105104

105+
const msg = [
106+
`Auth: user:${body.channel_data.user_id},`,
107+
`socket_id:${socket.id},`,
108+
`channel:${options.form.channel_name}`
109+
].join(' ');
110+
111+
Log.info(`[${new Date().toLocaleTimeString()}] - ` + msg);
112+
this.log.info(msg);
113+
106114
resolve(body);
107115
}
108116
});

src/echo-server.ts

+34-16
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,32 @@ export class EchoServer {
211211
this.server.io.on('connection', socket => {
212212
this.channel.joinRoot(socket) //Auth Root Channel '/'
213213
.then(auth => {
214-
if(auth === false)
215-
return IoUtils.disconnect(
216-
socket,
217-
this.log,
218-
'Laravel Auth Failed for user id:' + auth.channel_data.user_id,
219-
);
214+
if(auth === false) {
215+
const msg = `Auth: Failed for user:${auth.channel_data.user_id}, channel:root`;
216+
return IoUtils.disconnect(socket, this.log, msg);
217+
}
218+
219+
if(! auth.hasOwnProperty('channel_data') ){
220+
let msg_err = 'Error: on Connect Echo-Server response';
221+
msg_err += ' do not have channel_data property';
222+
this.log.error(msg_err);
223+
Log.error(msg_err);
224+
225+
return IoUtils.disconnect(socket, this.log, msg_err);
226+
} else if(! auth.channel_data.hasOwnProperty('user_id') ){
227+
let msg_err = 'Error: on Connect Echo-Server response';
228+
msg_err += ' do not have channel_data.user_id property';
229+
this.log.error(msg_err);
230+
Log.error(msg_err);
231+
232+
return IoUtils.disconnect(socket, this.log, msg_err);
233+
}
220234

221-
//console.log(socket.adapter.nsp.sockets)
222235
socket.user_id = auth.channel_data.user_id;
223236
const ip = IoUtils.getIp(socket, this.options);
224237

225238
Log.success(`LOG Success on Server: ${this.server.getServerId()}`);
226-
//IoUtils.setActiveUserOnServer(this.server.getServerId(),
227-
// {user_id: auth.channel_data.user_id, socket_id: socket.id})
228-
// collection echo_users, {user_id:1, socket_id:2ff, server_id: foo1 })
239+
229240
this.db.setUserInServer('echo_users', {
230241
user_id: auth.channel_data.user_id,
231242
socket_id: socket.id,
@@ -250,17 +261,24 @@ export class EchoServer {
250261
IoUtils.getAllActiveSocketsInThisIoServer(this.server.io)
251262
);
252263

253-
Log.success(`AUTH Success ON NSP / User Id:${socket.user_id} SocketID: ${socket.id} with IP:${ip}`);
254-
this.log.info(`Auth Success ON NSP / User Id:${socket.user_id} with Socket:${socket.id} with IP:${ip}`);
264+
const msg_sucess = [
265+
`Auth Success ON NSP /`,
266+
`User Id:${socket.user_id}`,
267+
`with Socket:${socket.id} with IP:${ip}`
268+
].join(' ');
269+
270+
Log.success(msg_sucess);
271+
this.log.info(msg_sucess);
272+
255273
return this.startSubscribers(socket);
256274

257275
})
258276
.catch(e => {
259-
Log.error(`Socket:${socket.id} join Root Auth Error, reason:${e.reason}`);
260-
this.log
261-
.error(`Socket:${socket.id} join Root Auth Error, reason:${e.reason}`);
277+
const msg_error = `Auth: Socket:${socket.id} join Root Auth Error, reason:${e.reason}`
278+
Log.error(msg_error);
279+
this.log.error(msg_error);
262280

263-
IoUtils.disconnect(socket, this.log, e.reason)
281+
IoUtils.disconnect(socket, this.log, msg_error)
264282
})
265283
});
266284
}

0 commit comments

Comments
 (0)