Created
October 2, 2019 15:12
-
-
Save raclim/c0e2caf7baa552596b755fc49e50ede1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https'); | |
var fs = require('fs'); // Using the filesystem module | |
var url = require('url'); | |
var options = { | |
key: fs.readFileSync('my-key.pem'), | |
cert: fs.readFileSync('my-cert.pem') | |
}; | |
function handleIt(req, res) { | |
var parsedUrl = url.parse(req.url); | |
var path = parsedUrl.pathname; | |
if (path == "/") { | |
path = "index.html"; | |
} | |
fs.readFile(__dirname + path, | |
// Callback function for reading | |
function (err, fileContents) { | |
// if there is an error | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading ' + req.url); | |
} | |
// Otherwise, send the data, the contents of the file | |
res.writeHead(200); | |
res.end(fileContents); | |
} | |
); | |
// Send a log message to the console | |
console.log("Got a request " + req.url); | |
} | |
var httpServer = https.createServer(options, handleIt); | |
httpServer.listen(8080); | |
var numUsers = 0; | |
// WebSocket Portion | |
// WebSockets work with the HTTP server | |
var io = require('socket.io').listen(httpServer); | |
// Register a callback function to run when we have an individual connection | |
// This is run for each individual user that connects | |
io.sockets.on('connection', | |
// We are given a websocket object in our function | |
function(socket) { | |
numUsers++; | |
console.log("We have a new client: " + socket.id); | |
socket.on('dataurl', function(data) { | |
io.sockets.emit('dataurl', data); | |
}); | |
socket.on('draw', | |
// Run this function when a message is sent | |
function(data) { | |
console.log(data); | |
// Call "broadcast" to send it to all clients (except sender), this is equal to | |
// socket.broadcast.emit('message', data); | |
//socket.broadcast.send(data); | |
// To all clients, on io.sockets instead | |
io.sockets.emit('draw', data); | |
} | |
); | |
// When this user emits, client side: socket.emit('otherevent',some data); | |
socket.on('otherevent', function(data) { | |
// Data comes in as whatever was sent, including objects | |
console.log("Received: 'otherevent' " + data); | |
}); | |
socket.on('disconnect', function() { | |
console.log("Client has disconnected"); | |
numUsers--; | |
}); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment