2021-06-27 01:41:52 +03:00
|
|
|
import express, { Request, Response } from "express";
|
2021-06-27 02:45:40 +03:00
|
|
|
import * as http from 'http';
|
|
|
|
|
import WebSocket from "ws"
|
2021-06-27 01:41:52 +03:00
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
2021-06-27 02:45:40 +03:00
|
|
|
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');
|
|
|
|
|
})
|
|
|
|
|
|
2021-06-27 01:41:52 +03:00
|
|
|
app.get("/", (req: Request, res: Response) => {
|
|
|
|
|
res.send("Hello World");
|
|
|
|
|
});
|
|
|
|
|
|
2021-06-27 02:45:40 +03:00
|
|
|
const port = process.env.PORT || 5000
|
|
|
|
|
|
|
|
|
|
server.listen(port, () => {
|
|
|
|
|
console.log(`Server started on port ${port} :)`);
|
|
|
|
|
})
|