1
-
2
- import readline from 'readline'
3
1
import { EventEmitter } from 'events'
4
2
import net from 'net'
5
3
import { randomBytes } from 'crypto'
@@ -10,7 +8,7 @@ function generateRandomId () {
10
8
}
11
9
12
10
export default class SocketServer extends EventEmitter {
13
- constructor ( port , newProtocol = ( ) => new Protocol ( ) , newId = generateRandomId ) {
11
+ constructor ( port , newProtocol = stream => new Protocol ( stream ) , newId = generateRandomId ) {
14
12
super ( )
15
13
this . _newProtocol = newProtocol
16
14
this . _server = null
@@ -21,28 +19,7 @@ export default class SocketServer extends EventEmitter {
21
19
22
20
start ( ) {
23
21
if ( ! this . _server ) {
24
- this . _server = net . createServer ( connection => {
25
- const contextId = this . _newId ( )
26
- console . log ( `[${ contextId } ] Creating connection` )
27
- const protocol = this . _newProtocol ( )
28
-
29
- this . _clients . set ( contextId , {
30
- protocol,
31
- connection
32
- } )
33
-
34
- this . emit ( 'contextOpen' , { contextId } )
35
- protocol . on ( 'request' , request => this . emit ( 'request' , { contextId, request } ) )
36
- protocol . on ( 'error' , e => this . writeBackendError ( contextId , e ) )
37
-
38
- connection . on ( 'end' , ( ) => {
39
- this . _clients . delete ( contextId )
40
- this . emit ( 'contextClose' , { contextId } )
41
- } )
42
-
43
- const iface = readline . createInterface ( connection , null )
44
- iface . on ( 'line' , protocol . processLine . bind ( protocol ) )
45
- } )
22
+ this . _server = net . createServer ( this . _handleConnection . bind ( this ) )
46
23
47
24
this . _server . listen ( this . _port , ( ) => {
48
25
console . log ( 'Listening' )
@@ -52,24 +29,50 @@ export default class SocketServer extends EventEmitter {
52
29
}
53
30
}
54
31
55
- writeResponse ( contextId , name , data ) {
32
+ _handleConnection ( connection ) {
33
+ console . log ( 'Backend connected' )
34
+
35
+ const contextId = this . _newId ( )
36
+ const protocol = this . _newProtocol ( connection )
37
+
38
+ this . _clients . set ( contextId , {
39
+ protocol,
40
+ connection
41
+ } )
42
+
43
+ this . emit ( 'contextOpen' , { contextId } )
44
+ protocol . on ( 'request' , request => this . emit ( 'request' , { contextId, request } ) )
45
+ protocol . on ( 'error' , e => this . writeBackendError ( contextId , e ) )
46
+
47
+ connection . on ( 'end' , ( ) => {
48
+ if ( this . _clients . has ( contextId ) ) {
49
+ this . _clients . get ( contextId ) . protocol . stop ( )
50
+ }
51
+ this . _clients . delete ( contextId )
52
+ this . emit ( 'contextClose' , { contextId } )
53
+ } )
54
+
55
+ protocol . start ( )
56
+ }
57
+
58
+ writeResponse ( contextId , response ) {
56
59
if ( this . _clients . has ( contextId ) ) {
57
60
const { protocol, connection } = this . _clients . get ( contextId )
58
- const chunk = protocol . serializeResponse ( name , data )
61
+ const chunk = protocol . serializeResponse ( response )
59
62
connection . write ( chunk , 'utf8' , ( ) => { } )
60
-
61
63
}
62
64
}
63
65
64
66
writeBackendError ( contextId , error ) {
65
- this . writeResponse ( contextId , 'BackendError' , { msg : error } )
67
+ this . writeResponse ( contextId , { name : 'BackendError' , data : { msg : error } } )
66
68
}
67
69
68
70
stop ( ) {
69
71
if ( this . _server ) {
70
72
this . _server . close ( )
73
+ this . _server = null
74
+ this . _clients . forEach ( client => client . protocol . stop ( ) )
71
75
this . _clients = new Map ( )
72
76
}
73
77
}
74
-
75
78
}
0 commit comments