File tree 5 files changed +59
-38
lines changed
5 files changed +59
-38
lines changed Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > Socket.IO Fiddle</ title >
6
+ </ head >
7
+ < body >
8
+ < p > Status: < span id ="status "> </ span > </ p >
9
+ < p > Transport: < span id ="transport "> </ span > </ p >
10
+
11
+ < script src ="/socket.io/socket.io.js "> </ script >
12
+ < script >
13
+ const statusSpan = document . getElementById ( "status" ) ;
14
+ const transportSpan = document . getElementById ( "transport" ) ;
15
+ const socket = io ( ) ;
16
+
17
+ statusSpan . innerText = "Disconnected" ;
18
+ transportSpan . innerText = "N/A" ;
19
+
20
+ socket . on ( "connect" , ( ) => {
21
+ statusSpan . innerText = "Connected" ;
22
+ transportSpan . innerText = socket . io . engine . transport . name ;
23
+ socket . io . engine . on ( "upgrade" , ( transport ) => {
24
+ transportSpan . innerText = transport . name ;
25
+ } ) ;
26
+ console . log ( `connect ${ socket . id } ` ) ;
27
+ } ) ;
28
+
29
+ socket . on ( "connect_error" , ( err ) => {
30
+ console . log ( `connect_error due to ${ err . message } ` ) ;
31
+ } ) ;
32
+
33
+ socket . on ( "disconnect" , ( reason ) => {
34
+ statusSpan . innerText = "Disconnected" ;
35
+ transportSpan . innerText = "N/A" ;
36
+ console . log ( `disconnect due to ${ reason } ` ) ;
37
+ } ) ;
38
+ </ script >
39
+ </ body >
40
+ </ html >
Original file line number Diff line number Diff line change 6
6
"license" : " MIT" ,
7
7
"type" : " module" ,
8
8
"dependencies" : {
9
- "express" : " ~4.17.1" ,
10
9
"socket.io" : " ^4.0.0" ,
11
10
"socket.io-client" : " ^4.0.0"
12
11
},
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- import { default as express } from "express " ;
2
- import { createServer } from "http " ;
1
+ import { createServer } from "node:http " ;
2
+ import { readFile } from "node:fs/promises " ;
3
3
import { Server } from "socket.io" ;
4
4
5
- const app = express ( ) ;
6
- const httpServer = createServer ( app ) ;
7
- const io = new Server ( httpServer , { } ) ;
8
-
9
5
const port = process . env . PORT || 3000 ;
10
6
11
- app . use ( express . static ( "public" ) ) ;
7
+ const httpServer = createServer ( async ( req , res ) => {
8
+ if ( req . url !== "/" ) {
9
+ res . writeHead ( 404 ) ;
10
+ res . end ( "Not found" ) ;
11
+ return ;
12
+ }
13
+ // reload the file every time
14
+ const content = await readFile ( "index.html" ) ;
15
+
16
+ res . writeHead ( 200 , {
17
+ "Content-Type" : "text/html" ,
18
+ "Content-Length" : Buffer . byteLength ( content ) ,
19
+ } ) ;
20
+ res . end ( content ) ;
21
+ } ) ;
22
+
23
+ const io = new Server ( httpServer , { } ) ;
12
24
13
25
io . on ( "connection" , ( socket ) => {
14
26
console . log ( `connect ${ socket . id } ` ) ;
You can’t perform that action at this time.
0 commit comments