File tree 3 files changed +16
-7
lines changed
3 files changed +16
-7
lines changed Original file line number Diff line number Diff line change
1
+ import { io } from "socket.io-client" ;
1
2
2
- const socket = require ( "socket.io-client" ) ( "http://localhost:3000" ) ;
3
+ const port = process . env . PORT || 3000 ;
4
+
5
+ const socket = io ( `http://localhost:${ port } ` ) ;
3
6
4
7
socket . on ( "connect" , ( ) => {
5
8
console . log ( `connect ${ socket . id } ` ) ;
Original file line number Diff line number Diff line change 4
4
"description" : " Simple fiddle for socket.io" ,
5
5
"author" : " Damien Arrachequesne" ,
6
6
"license" : " MIT" ,
7
+ "type" : " module" ,
7
8
"dependencies" : {
8
9
"express" : " ~4.17.1" ,
9
10
"socket.io" : " ^4.0.0" ,
Original file line number Diff line number Diff line change
1
+ import { default as express } from "express" ;
2
+ import { createServer } from "http" ;
3
+ import { Server } from "socket.io" ;
1
4
2
- const express = require ( "express" ) ;
3
5
const app = express ( ) ;
4
- const server = require ( "http" ) . createServer ( app ) ;
5
- const io = require ( "socket.io" ) ( server ) ;
6
+ const httpServer = createServer ( app ) ;
7
+ const io = new Server ( httpServer , { } ) ;
8
+
6
9
const port = process . env . PORT || 3000 ;
7
10
8
- app . use ( express . static ( __dirname + "/ public") ) ;
11
+ app . use ( express . static ( " public") ) ;
9
12
10
- io . on ( "connection" , socket => {
13
+ io . on ( "connection" , ( socket ) => {
11
14
console . log ( `connect ${ socket . id } ` ) ;
12
15
13
16
socket . on ( "disconnect" , ( reason ) => {
14
17
console . log ( `disconnect ${ socket . id } due to ${ reason } ` ) ;
15
18
} ) ;
16
19
} ) ;
17
20
18
- server . listen ( port , ( ) => console . log ( `server listening at http://localhost:${ port } ` ) ) ;
21
+ httpServer . listen ( port , ( ) => {
22
+ console . log ( `server listening at http://localhost:${ port } ` ) ;
23
+ } ) ;
You can’t perform that action at this time.
0 commit comments