File tree 2 files changed +8
-8
lines changed
2 files changed +8
-8
lines changed Original file line number Diff line number Diff line change 1
1
2
2
const port = process . env . PORT || 3000 ;
3
- const socket = require ( "socket.io-client" ) ( `http://localhost:${ port } ` ) ;
3
+ const namespace = process . env . NSP || "test" ;
4
+ const socket = require ( "socket.io-client" ) ( `http://localhost:${ port } /${ namespace } ` ) ;
4
5
5
6
socket . on ( "connect" , ( ) => {
6
7
console . log ( `connect ${ socket . id } ` ) ;
@@ -14,6 +15,6 @@ socket.on("disconnect", (reason) => {
14
15
console . log ( `disconnect due to ${ reason } ` ) ;
15
16
} ) ;
16
17
17
- socket . on ( "ping " , ( time ) => {
18
- console . log ( `got ping : ${ time } ` ) ;
18
+ socket . on ( "new user " , ( id ) => {
19
+ console . log ( `new user : ${ id } ` ) ;
19
20
} ) ;
Original file line number Diff line number Diff line change @@ -15,12 +15,11 @@ const port = process.env.PORT || 3000;
15
15
16
16
app . use ( express . static ( __dirname + "/public" ) ) ;
17
17
18
- io . on ( "connection" , socket => {
19
- console . log ( `connect ${ socket . id } ` ) ;
18
+ io . of ( / .* / ) . on ( "connection" , ( socket ) => {
19
+ const namespace = socket . nsp ;
20
+ console . log ( `connect ${ socket . id } to ${ socket . nsp . name } ` ) ;
20
21
21
- socket . on ( "disconnect" , ( reason ) => {
22
- console . log ( `disconnect ${ socket . id } due to ${ reason } ` ) ;
23
- } ) ;
22
+ namespace . emit ( "new user" , socket . id ) ;
24
23
} ) ;
25
24
26
25
server . listen ( port , ( ) => console . log ( `server listening at http://localhost:${ port } ` ) ) ;
You can’t perform that action at this time.
0 commit comments