tested websocket

This commit is contained in:
jhalitaksoy 2021-06-27 02:45:40 +03:00
parent 9cac3a1b8f
commit 7829e53ecf
3 changed files with 36 additions and 4 deletions

16
package-lock.json generated
View File

@ -94,8 +94,7 @@
"@types/node": {
"version": "15.12.4",
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz",
"integrity": "sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA==",
"dev": true
"integrity": "sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA=="
},
"@types/qs": {
"version": "6.9.6",
@ -119,6 +118,14 @@
"@types/node": "*"
}
},
"@types/ws": {
"version": "7.4.5",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-7.4.5.tgz",
"integrity": "sha512-8mbDgtc8xpxDDem5Gwj76stBDJX35KQ3YBoayxlqUQcL5BZUthiqP/VQ4PQnLHqM4PmlbyO74t98eJpURO+gPA==",
"requires": {
"@types/node": "*"
}
},
"abbrev": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -1507,6 +1514,11 @@
"typedarray-to-buffer": "^3.1.5"
}
},
"ws": {
"version": "7.5.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.5.0.tgz",
"integrity": "sha512-6ezXvzOZupqKj4jUqbQ9tXuJNo+BR2gU8fFRk3XCP3e0G6WT414u5ELe6Y0vtp7kmSJ3F7YWObSNr1ESsgi4vw=="
},
"xdg-basedir": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz",

View File

@ -12,7 +12,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
"@types/ws": "^7.4.5",
"express": "^4.17.1",
"ws": "^7.5.0"
},
"devDependencies": {
"@types/express": "^4.17.12",

View File

@ -1,9 +1,27 @@
import express, { Request, Response } from "express";
import * as http from 'http';
import WebSocket from "ws"
const app = express();
const server = http.createServer(app);
const ws = new WebSocket.Server({server})
ws.on("connection", (ws : WebSocket)=>{
ws.on("message", (message : string)=>{
console.log('received: %s', message);
ws.send(`Hello, you sent -> ${message}`);
})
ws.send('Hi there, I am a WebSocket server');
})
app.get("/", (req: Request, res: Response) => {
res.send("Hello World");
});
app.listen(5000, () => console.log("Server listening on http://localhost:5000"))
const port = process.env.PORT || 5000
server.listen(port, () => {
console.log(`Server started on port ${port} :)`);
})