11import dgram from "dgram" ;
2- const port : number = 8080 ;
3-
42import express , { Application , Request , Response } from "express" ;
3+
54const app : Application = express ( ) ;
5+ const port : number = 8080 ;
6+
7+ /** HTTP Server for checking status */
68app . get ( '/' , ( req : Request , res : Response ) => { ``
79 res . send ( { message : 'Server is active' , timestamp : new Date ( ) } ) ;
810} ) ;
911
1012const Server = dgram . createSocket ( 'udp4' ) ;
1113
12- /** Broker listen to all subscribers and manage protocol */
13-
1414Server . on ( 'error' , ( err ) => {
1515 console . log ( `Server error:\n${ err . stack } ` ) ;
1616 Server . close ( ) ;
1717} ) ;
1818
19+ /** Receive Messages */
1920Server . on ( 'message' , ( msg , rinfo ) => {
2021 console . log ( `Server got: ${ msg } from ${ rinfo . address } :${ rinfo . port } ` ) ;
2122
@@ -30,13 +31,11 @@ Server.on('message', (msg, rinfo) => {
3031
3132/** Launch server and listen on given port */
3233try {
33-
3434 Server . on ( 'listening' , ( ) => {
3535 const address = Server . address ( ) ;
3636 console . log ( `Server listening ${ address . address } :${ address . port } ` ) ;
3737 } ) ;
3838
39-
4039 Server . bind ( port , ( ) : void => {
4140 console . log ( `UDP Datagram Server is active at http://localhost:${ port } ` ) ;
4241 } ) ;
0 commit comments