1/*
2Author: Logan Smith - Perkins
3*/
4
5// Importing the http library, used to start servers
6const http = require('http');
7// Importing the websocket library which is used to interface between webpage and nodejs
8const WebSocketServer = require('websocket').server;
9// This server is created using the http createServer function, which enables the user to create a http connection with a webpage
10const server =- http.createServer();
11// The server then listens on the port specified
12server.listen(7000);
13
14// We then create a new variable which will store the actual server I'll be running
15const wsServer = new WebSocketServer({
16 // Then we set the parameter of httpServer to the server variable that we said that would be listening on the port specified
17 httpServer : server
18});
19
20// Next we check if someome is trying to connect to the server, i.e. the name request, it's requesting access to the server
21wsServer.on('request', function(request){
22 // We store the actual connection as a variable and we accept that client to connect to this server
23 const connection = request.accept(null, request.origin);
24 // This function is run when this client sends a message to the server.
25 connection.on('message', function(message){
26 // We print out to the console the recieved message decoded to utf8
27 console.log("Recieved Message: " + message.utf8Data);
28 // Then we send specifically to this connection back a message
29 connection.sendUTF("Hello this is the websocket server.");
30 });
31 // This code is run when the user disconnects from the server.
32 connection.on('close', function(reasonCode, description){
33 // We just print to the console that a client has disconnected from the server.
34 console.log("A client has disconnected.");
35 });
36});