@@ -24,6 +24,8 @@ app.post("/incr", (req, res) => {
24
24
const session = req . session ;
25
25
session . count = ( session . count || 0 ) + 1 ;
26
26
res . status ( 200 ) . end ( "" + session . count ) ;
27
+
28
+ io . to ( session . id ) . emit ( "current count" , session . count ) ;
27
29
} ) ;
28
30
29
31
app . post ( "/logout" , ( req , res ) => {
@@ -35,37 +37,9 @@ app.post("/logout", (req, res) => {
35
37
} ) ;
36
38
} ) ;
37
39
38
- const io = new Server ( httpServer , {
39
- allowRequest : ( req , callback ) => {
40
- // with HTTP long-polling, we have access to the HTTP response here, but this is not
41
- // the case with WebSocket, so we provide a dummy response object
42
- const fakeRes = {
43
- getHeader ( ) {
44
- return [ ] ;
45
- } ,
46
- setHeader ( key , values ) {
47
- req . cookieHolder = values [ 0 ] ;
48
- } ,
49
- writeHead ( ) { } ,
50
- } ;
51
- sessionMiddleware ( req , fakeRes , ( ) => {
52
- if ( req . session ) {
53
- // trigger the setHeader() above
54
- fakeRes . writeHead ( ) ;
55
- // manually save the session (normally triggered by res.end())
56
- req . session . save ( ) ;
57
- }
58
- callback ( null , true ) ;
59
- } ) ;
60
- } ,
61
- } ) ;
40
+ const io = new Server ( httpServer ) ;
62
41
63
- io . engine . on ( "initial_headers" , ( headers , req ) => {
64
- if ( req . cookieHolder ) {
65
- headers [ "set-cookie" ] = req . cookieHolder ;
66
- delete req . cookieHolder ;
67
- }
68
- } ) ;
42
+ io . engine . use ( sessionMiddleware ) ;
69
43
70
44
io . on ( "connection" , ( socket ) => {
71
45
const req = socket . request ;
0 commit comments