-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented TCPServer for socket handling.
- Loading branch information
1 parent
8d10793
commit 71074ed
Showing
3 changed files
with
79 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as net from 'net' | ||
|
||
const EventEmitter = require('events'); | ||
|
||
export class TCPServer extends EventEmitter { | ||
private readonly server: net.Server; | ||
private readonly connections: Array<net.Socket>; | ||
private readonly port: number; | ||
|
||
constructor(port: number) { | ||
super(); | ||
|
||
this.connections = new Array<net.Socket>(); | ||
this.port = port; | ||
|
||
this.server = net.createServer((socket) => { | ||
this.connections.push(socket); | ||
|
||
socket.on('data', (data) => { | ||
console.log("Data: " + data); | ||
this.sendMessage("Nachricht"); | ||
}); | ||
|
||
socket.on('connection', (data) => { | ||
console.log("new connection"); | ||
}); | ||
|
||
socket.on('close', (data: net.Socket) => { | ||
const index: number = this.connections.indexOf(socket, 0); | ||
if (index > -1) { | ||
this.connections.splice(index, 1); | ||
} | ||
|
||
console.log("connection closed"); | ||
}); | ||
|
||
socket.on('end', () => { | ||
console.log("end"); | ||
}); | ||
|
||
socket.on('timeout', () => { | ||
console.log("timeout"); | ||
}); | ||
|
||
}); | ||
|
||
this.server.listen(this.port, function () { | ||
console.log(`Server listening for connection requests on socket localhost:3000`); | ||
}); | ||
} | ||
|
||
sendMessage(message: string) { | ||
let socket: net.Socket; | ||
for (socket of this.connections) { | ||
socket.write(message); | ||
} | ||
} | ||
} |