Skip to content

Commit de273a4

Browse files
Merge pull request #1 from mangledbottles/datagram
Datagram Socket API Initial solution utilized Socket.io which works on the TCP protocol and this assignment requires use of the UDP protocol. This merge is an implementation of this assignment using the UDP protocol.
2 parents b26152e + 8ad166a commit de273a4

10 files changed

Lines changed: 96 additions & 70 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

App/index.html

Lines changed: 0 additions & 18 deletions
This file was deleted.

Client.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import dgram from 'dgram';
2+
import { Buffer } from 'buffer';
3+
const port: number = 8080;
4+
5+
const message = Buffer.from('UDP IOT CONNETION DATA');
6+
const client = dgram.createSocket('udp4');
7+
8+
client.on('message', (msg, info) => {
9+
console.log(`Data received from server: ${msg.toString()}`);
10+
console.log(`Received ${msg.length} bytes from ${info.address}:${info.port}\n`);
11+
});
12+
13+
client.send(message, port, 'localhost', (err) => {
14+
if (err) {
15+
console.log('Error sending data to Server')
16+
client.close();
17+
} else {
18+
console.log('Data sent to Server')
19+
}
20+
});

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM node:16.10.0
2+
3+
WORKDIR /
4+
5+
COPY package*.json ./
6+
7+
RUN npm install
8+
9+
# Install Typescript dependecies
10+
RUN npm install -g ts-node typescript
11+
12+
COPY . .
13+
14+
ENV PORT=8081
15+
16+
# HTTP Server Port
17+
EXPOSE 8081
18+
19+
# UDP Datagram Server Port
20+
EXPOSE 8080/udp
21+
22+
# Start Broker Server
23+
CMD [ "npm", "run", "server" ]

Server.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import dgram from "dgram";
2+
import express, { Application, Request, Response } from "express";
3+
4+
const app: Application = express();
5+
const port: number = 8080;
6+
7+
/** HTTP Server for checking status */
8+
app.get('/', (req: Request, res: Response) => {``
9+
res.send({ message: 'Server is active', timestamp: new Date() });
10+
});
11+
12+
const Server = dgram.createSocket('udp4');
13+
14+
Server.on('error', (err) => {
15+
console.log(`Server error:\n${err.stack}`);
16+
Server.close();
17+
});
18+
19+
/** Receive Messages */
20+
Server.on('message', (msg, rinfo) => {
21+
console.log(`Server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
22+
23+
Server.send(msg, rinfo.port, 'localhost', function (error) {
24+
if (error) {
25+
console.log(`Error sending data to Client #${rinfo.port}`)
26+
} else {
27+
console.log(`Data sent to Client #${rinfo.port}`);
28+
}
29+
});
30+
});
31+
32+
/** Launch server and listen on given port */
33+
try {
34+
Server.on('listening', () => {
35+
const address = Server.address();
36+
console.log(`Server listening ${address.address}:${address.port}`);
37+
});
38+
39+
Server.bind(port, (): void => {
40+
console.log(`UDP Datagram Server is active at http://localhost:${port}`);
41+
});
42+
43+
app.listen(8081, (): void => {
44+
console.log(`HTTP Server is active at http://localhost:8081`);
45+
});
46+
} catch (error: any) {
47+
console.error(`An error occurred with message ${error.toString()}`);
48+
}

Server/Index.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

command.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker run -p 8080:8080/udp 433be8938248
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"start": "npm run dev",
88
"build": "npm run clean && tsc",
9-
"dev": "nodemon Index.ts",
9+
"server": "nodemon Server.ts",
10+
"client": "nodemon Client.ts",
1011
"dev-js": "nodemon Index.js",
1112
"clean": "rimraf ./dist",
1213
"reset": "git pull && git checkout . && npm i && npm run build"
@@ -20,12 +21,10 @@
2021
"devDependencies": {
2122
"@types/express": "^4.17.13",
2223
"@types/node": "^16.10.1",
23-
"@types/socket.io": "^3.0.2",
2424
"nodemon": "^2.0.13",
2525
"typescript": "^4.4.3"
2626
},
2727
"dependencies": {
28-
"express": "^4.17.1",
29-
"socket.io": "^4.2.0"
28+
"express": "^4.17.1"
3029
}
3130
}
File renamed without changes.

0 commit comments

Comments
 (0)