"nodejs websocket tutorial" Code Answer's

You're definitely familiar with the best coding language Javascript that developers use to develop their projects and they get all their queries like "nodejs websocket tutorial" answered properly. Developers are finding an appropriate answer about nodejs websocket tutorial related to the Javascript coding language. By visiting this online portal developers get answers concerning Javascript codes question like nodejs websocket tutorial. Enter your desired code related query in the search bar and get every piece of information about Javascript code related question on nodejs websocket tutorial. 

nodejs websocket tutorial

By Silly SalamanderSilly Salamander on Feb 16, 2020
/*
Author: Logan Smith - Perkins
*/

// Importing the http library, used to start servers
const http = require('http');
// Importing the websocket library which is used to interface between webpage and nodejs
const WebSocketServer = require('websocket').server;
// This server is created using the http createServer function, which enables the user to create a http connection with a webpage
const server =- http.createServer();
// The server then listens on the port specified
server.listen(7000);

// We then create a new variable which will store the actual server I'll be running
const wsServer = new WebSocketServer({
	// Then we set the parameter of httpServer to the server variable that we said that would be listening on the port specified
	httpServer : server
});

// Next we check if someome is trying to connect to the server, i.e. the name request, it's requesting access to the server
wsServer.on('request', function(request){
	// We store the actual connection as a variable and we accept that client to connect to this server
	const connection = request.accept(null, request.origin);
	// This function is run when this client sends a message to the server.
	connection.on('message', function(message){
		// We print out to the console the recieved message decoded to utf8
		console.log("Recieved Message: " + message.utf8Data);
		// Then we send specifically to this connection back a message
		connection.sendUTF("Hello this is the websocket server.");
	});
	// This code is run when the user disconnects from the server.
	connection.on('close', function(reasonCode, description){
		// We just print to the console that a client has disconnected from the server.
		console.log("A client has disconnected.");
	});
});

Add Comment

5

All those coders who are working on the Javascript based application and are stuck on nodejs websocket tutorial can get a collection of related answers to their query. Programmers need to enter their query on nodejs websocket tutorial related to Javascript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about nodejs websocket tutorial for the programmers working on Javascript code while coding their module. Coders are also allowed to rectify already present answers of nodejs websocket tutorial while working on the Javascript language code. Developers can add up suggestions if they deem fit any other answer relating to "nodejs websocket tutorial". Visit this developer's friendly online web community, CodeProZone, and get your queries like nodejs websocket tutorial resolved professionally and stay updated to the latest Javascript updates. 

Javascript answers related to "nodejs websocket tutorial"

View All Javascript queries

Javascript queries related to "nodejs websocket tutorial"

Browse Other Code Languages

CodeProZone