Skip to content

Instantly share code, notes, and snippets.

@bshambaugh
Created November 5, 2021 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bshambaugh/6c84e52a7281b87d5c05b8df963bdbc9 to your computer and use it in GitHub Desktop.
Save bshambaugh/6c84e52a7281b87d5c05b8df963bdbc9 to your computer and use it in GitHub Desktop.
simple websocket server
/**************************websocket_example.js*************************************************/
var bodyParser = require("body-parser");
const express = require('express'); //express framework to have a higher level of methods
const app = express(); //assign app variable the express class/method
var http = require('http');
var path = require("path");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const server = http.createServer(app);//create a server
//***************this snippet gets the local ip of the node.js server. copy this ip to the client side code and add ':3000' *****
//****************exmpl. 192.168.56.1---> var sock =new WebSocket("ws://192.168.56.1:3000");*************************************
require('dns').lookup(require('os').hostname(), function (err, add, fam) {
console.log('addr: '+add);
})
/**********************websocket setup**************************************************************************************/
//var expressWs = require('express-ws')(app,server);
const WebSocket = require('ws');
const s = new WebSocket.Server({ server });
//when browser sends get request, send html file to browser
// viewed at http://localhost:30000
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
//*************************************************************************************************************************
//***************************ws chat server********************************************************************************
//app.ws('/echo', function(ws, req) {
s.on('connection',function(ws,req){
ws.on('close', function(){
console.log("lost one client");
});
ws.send("new client connected");
console.log("new client connected");
var interval = setInterval(function(){
console.log('Hello World');
ws.send("toggleLED");
}, 2000);
});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment