Added user (dis)connection broadcasts
This commit is contained in:
parent
1b77a5511c
commit
c8c4ca463e
11
index.html
11
index.html
@ -21,17 +21,16 @@
|
|||||||
|
|
||||||
// Wait for server response and identify client
|
// Wait for server response and identify client
|
||||||
socket.on('user_identify', function(){
|
socket.on('user_identify', function(){
|
||||||
socket.emit('user_login', 'Bart');
|
socket.emit('user_login', {
|
||||||
|
'username': 'Bart',
|
||||||
|
'mailHash': '289e66591abdb9c2d4cb15517f0fd8d8'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send data to server on form submit
|
// Send data to server on form submit
|
||||||
$('form').submit(function(e){
|
$('form').submit(function(e){
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var msg = {
|
socket.emit('message_send', $('#message').val());
|
||||||
'username': $('#username').val(),
|
|
||||||
'message': $('#message').val()
|
|
||||||
}
|
|
||||||
socket.emit('message_send', msg);
|
|
||||||
$('#message').val('');
|
$('#message').val('');
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
37
index.js
37
index.js
@ -7,6 +7,15 @@ var io = require('socket.io')(http)
|
|||||||
const port = 9966;
|
const port = 9966;
|
||||||
var users = 0;
|
var users = 0;
|
||||||
|
|
||||||
|
// Function for easy server broadcasts
|
||||||
|
function serverMsg(content) {
|
||||||
|
return {
|
||||||
|
'username': 'Server',
|
||||||
|
'mailHash': '470965550d3f019830c421f0535d9268',
|
||||||
|
'message': content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Serve Client
|
// Serve Client
|
||||||
app.get('/', function(req, res){
|
app.get('/', function(req, res){
|
||||||
res.sendFile(__dirname + '/index.html');
|
res.sendFile(__dirname + '/index.html');
|
||||||
@ -14,29 +23,37 @@ app.get('/', function(req, res){
|
|||||||
|
|
||||||
// Socket.io server side
|
// Socket.io server side
|
||||||
io.on('connection', function (socket) {
|
io.on('connection', function (socket) {
|
||||||
// Log connects / disconnects
|
// Send identification request to client
|
||||||
console.log('User connected');
|
|
||||||
socket.emit('user_identify');
|
socket.emit('user_identify');
|
||||||
|
|
||||||
socket.on('user_login', function (username) {
|
// Bind username and mailhash to client
|
||||||
io.emit('message_receive', { 'username': 'Server', 'message': `${username} has connected`});
|
socket.on('user_login', function (user) {
|
||||||
|
socket.username = user.username;
|
||||||
|
socket.mailHash = user.mailHash;
|
||||||
|
io.emit('message_receive', serverMsg(`${socket.username} has connected`));
|
||||||
|
users++;
|
||||||
});
|
});
|
||||||
|
|
||||||
users++;
|
// Send disconnect broadcast to all clients
|
||||||
socket.on('disconnect', function () {
|
socket.on('disconnect', function () {
|
||||||
console.log('User disconnected');
|
io.emit('message_receive', serverMsg(`${socket.username} has disconnected`));
|
||||||
users--;
|
users--;
|
||||||
});
|
});
|
||||||
console.log(`Users online: ${users}`);
|
|
||||||
|
|
||||||
// Handle messages
|
// Handle messages
|
||||||
socket.on('message_send', function (msg) {
|
socket.on('message_send', function (message) {
|
||||||
console.log(msg.username + ': ' + msg.message);
|
// Build message data for clients
|
||||||
|
var msg = {
|
||||||
|
'username': socket.username,
|
||||||
|
'mailHash': socket.mailHash,
|
||||||
|
'message': message
|
||||||
|
}
|
||||||
|
// Send message to clients
|
||||||
io.emit('message_receive', msg);
|
io.emit('message_receive', msg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Log on server
|
// Start http server
|
||||||
http.listen(port, function() {
|
http.listen(port, function() {
|
||||||
console.log(`Listening on port ${port}`);
|
console.log(`Listening on port ${port}`);
|
||||||
});
|
});
|
Loading…
Reference in New Issue
Block a user