File tree Expand file tree Collapse file tree 4 files changed +19
-8
lines changed Expand file tree Collapse file tree 4 files changed +19
-8
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const json = require('koa-json');
4
4
const onerror = require ( 'koa-onerror' ) ;
5
5
const bodyparser = require ( 'koa-bodyparser' ) ;
6
6
const logger = require ( 'koa-logger' ) ;
7
+ const sseHeader = require ( './middlewares/sseHeader' ) ;
7
8
const cors = require ( './middlewares/cors' ) ;
8
9
const db = require ( './libs/db' ) ;
9
10
@@ -37,8 +38,12 @@ app.use(async (ctx, next) => {
37
38
// set CORS header
38
39
app . use ( cors ( ) ) ;
39
40
41
+ // set sse headers
42
+ app . use ( sseHeader ( ) ) ;
43
+
40
44
// routes
41
45
app . use ( routeClient . routes ( ) , routeClient . allowedMethods ( ) ) ;
42
46
app . use ( routeServer . routes ( ) , routeServer . allowedMethods ( ) ) ;
43
47
48
+
44
49
module . exports = app ;
Original file line number Diff line number Diff line change
1
+ module . exports = ( ) => async ( ctx , next ) => {
2
+ if ( / ^ \/ a l i v e / . test ( ctx . path ) ) {
3
+ // otherwise node will automatically close this connection in 10 minutes.
4
+ ctx . req . setTimeout ( 1000 * 60 * 10 ) ;
5
+ ctx . type = 'text/event-stream; charset=utf-8' ;
6
+ ctx . set ( 'Cache-Control' , 'no-cache' ) ;
7
+ ctx . set ( 'Connection' , 'keep-alive' ) ;
8
+ ctx . set ( 'X-Accel-Buffering' , 'no' ) ;
9
+ }
10
+ await next ( ) ;
11
+ } ;
Original file line number Diff line number Diff line change 10
10
Hello World!
11
11
</ body >
12
12
< script >
13
- const url = '/test' ;
13
+ const url = '/alive/ test' ;
14
14
var source = new EventSource ( url ) ;
15
15
source . addEventListener ( 'open' , function ( event ) {
16
16
console . log ( 'open' ) ;
Original file line number Diff line number Diff line change 5
5
const router = require ( 'koa-router' ) ( ) ;
6
6
const sse = require ( '../libs/sse' ) ;
7
7
8
+ router . prefix ( '/alive' ) ;
9
+
8
10
router
9
11
. get ( '/test' , async ( ctx ) => {
10
12
// otherwise node will automatically close this connection in 10 minutes.
11
- ctx . req . setTimeout ( 1000 * 60 * 10 ) ;
12
- ctx . type = 'text/event-stream; charset=utf-8' ;
13
- ctx . set ( 'Cache-Control' , 'no-cache' ) ;
14
- ctx . set ( 'Connection' , 'keep-alive' ) ;
15
- ctx . set ( 'X-Accel-Buffering' , 'no' ) ;
16
-
17
13
const body = ctx . body = new sse ( ) ;
18
-
19
14
const ssid = ctx . cookies . get ( 'ssid' ) ;
20
15
if ( ssid ) {
21
16
global . table [ ssid ] = body ;
You can’t perform that action at this time.
0 commit comments