tested websocket
This commit is contained in:
parent
9cac3a1b8f
commit
7829e53ecf
16
package-lock.json
generated
16
package-lock.json
generated
@ -94,8 +94,7 @@
|
|||||||
"@types/node": {
|
"@types/node": {
|
||||||
"version": "15.12.4",
|
"version": "15.12.4",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz",
|
||||||
"integrity": "sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA==",
|
"integrity": "sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"@types/qs": {
|
"@types/qs": {
|
||||||
"version": "6.9.6",
|
"version": "6.9.6",
|
||||||
@ -119,6 +118,14 @@
|
|||||||
"@types/node": "*"
|
"@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": {
|
"abbrev": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
|
||||||
@ -1507,6 +1514,11 @@
|
|||||||
"typedarray-to-buffer": "^3.1.5"
|
"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": {
|
"xdg-basedir": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz",
|
||||||
|
|||||||
@ -12,7 +12,9 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.17.1"
|
"@types/ws": "^7.4.5",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"ws": "^7.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/express": "^4.17.12",
|
"@types/express": "^4.17.12",
|
||||||
|
|||||||
20
src/index.ts
20
src/index.ts
@ -1,9 +1,27 @@
|
|||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
|
import * as http from 'http';
|
||||||
|
import WebSocket from "ws"
|
||||||
|
|
||||||
const app = express();
|
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) => {
|
app.get("/", (req: Request, res: Response) => {
|
||||||
res.send("Hello World");
|
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} :)`);
|
||||||
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user