-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsocket.js
More file actions
24 lines (21 loc) · 709 Bytes
/
socket.js
File metadata and controls
24 lines (21 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"use strict";
const serverPort = 3000;
const http = require("http");
const express = require("express");
const app = express();
const server = http.createServer(app)
const WebSocket = require("ws");
const websocketServer = new WebSocket.Server({ server });
//when a websocket connection is established
websocketServer.on("connection", (webSocketClient) => {
// send feedback to the incoming connection
webSocketClient.send("The time is: ");
setInterval(() => {
let time = new Date();
webSocketClient.send("the time is: " + time.toTimeString());
}, 1000);
});
//start the web server
server.listen(serverPort, () => {
console.log(`Websocket server started on port ` + serverPort);
});