1-
2- import readline from 'readline'
31import { EventEmitter } from 'events'
42import net from 'net'
53import { randomBytes } from 'crypto'
@@ -10,7 +8,7 @@ function generateRandomId () {
108}
119
1210export default class SocketServer extends EventEmitter {
13- constructor ( port , newProtocol = ( ) => new Protocol ( ) , newId = generateRandomId ) {
11+ constructor ( port , newProtocol = stream => new Protocol ( stream ) , newId = generateRandomId ) {
1412 super ( )
1513 this . _newProtocol = newProtocol
1614 this . _server = null
@@ -21,28 +19,7 @@ export default class SocketServer extends EventEmitter {
2119
2220 start ( ) {
2321 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 ) )
4623
4724 this . _server . listen ( this . _port , ( ) => {
4825 console . log ( 'Listening' )
@@ -52,24 +29,50 @@ export default class SocketServer extends EventEmitter {
5229 }
5330 }
5431
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 ) {
5659 if ( this . _clients . has ( contextId ) ) {
5760 const { protocol, connection } = this . _clients . get ( contextId )
58- const chunk = protocol . serializeResponse ( name , data )
61+ const chunk = protocol . serializeResponse ( response )
5962 connection . write ( chunk , 'utf8' , ( ) => { } )
60-
6163 }
6264 }
6365
6466 writeBackendError ( contextId , error ) {
65- this . writeResponse ( contextId , 'BackendError' , { msg : error } )
67+ this . writeResponse ( contextId , { name : 'BackendError' , data : { msg : error } } )
6668 }
6769
6870 stop ( ) {
6971 if ( this . _server ) {
7072 this . _server . close ( )
73+ this . _server = null
74+ this . _clients . forEach ( client => client . protocol . stop ( ) )
7175 this . _clients = new Map ( )
7276 }
7377 }
74-
7578}
0 commit comments