From fb0fa1995688b227f6c250ff516a30e68368eee8 Mon Sep 17 00:00:00 2001 From: Antiik91 Date: Tue, 3 Jan 2017 15:35:08 +0200 Subject: [PATCH 1/3] Started with multiroom support socket.get method's are gone, will have to look a new way to use them --- index.html | 20 ++++++++++++++++++-- index.js | 33 ++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 2fd1a17..2b8c885 100644 --- a/index.html +++ b/index.html @@ -16,6 +16,10 @@
+

Tervetuloa Tänne :)

@@ -30,12 +34,17 @@ diff --git a/index.js b/index.js index ea2b9e0..9772402 100644 --- a/index.js +++ b/index.js @@ -7,28 +7,51 @@ app.get('/', function(req, res){ }); var clients = []; -io.on('connection', function(socket) { +io.sockets.on('connection', function(socket) { console.log('a user is connected'); socket.on('setUsername', function(data){ if(clients.indexOf(data) > -1) { socket.emit('userTaken', data + ' Valitettavasti on jo varattu'); // console.log(data +' varattu'); - } - else { + } else { clients.push(data); socket.emit('setUser', {username: data}); // console.log(data +' Vapaana'); io.sockets.emit('broadcast', {description: data + ' Liittyi kanavalle'}); } }); + + socket.on('join', function(channel, ack) { + socket.get('channel', function(err,oldChannel){ + if(oldChannel) { + socket.leave('oldChannel'); + } + socket.set('channel', channel, function() { + socket.join(channel); + ack(); + }); + }); + }); socket.on('disconnect', function(data){ var u = clients.indexOf(data); clients.splice(u, 1); console.log(' user disconnected'); io.sockets.emit('broadcast', {description: 'Käyttäjä lähti kanavalta'}); }); - socket.on('chat message', function(user, data){ - io.sockets.emit('broadcast', {description: user +": "+ data}); + socket.on('chat message', function(user, data, ack){ + socket.get('channel', function(err,channel){ + if(err) { + socket.emit('error',err); + } + else if(channel) { + io.socket.broadcast.to(channel).emit('broadcast', {description: user +": "+ data}); + ack(); + } else { + socket.emit('error', 'Ei kanavaa valittuna'); + console.log('Something happened in chat message section.'); + } + }); + // console.log('viesti ' + user + " "+ data); }); }); From f7558094f322d57b07f475a898d9060be84b4c53 Mon Sep 17 00:00:00 2001 From: Antiik91 Date: Sun, 8 Jan 2017 15:50:13 +0200 Subject: [PATCH 2/3] Some progress, currently passing channel is null so messages comes to the server but not to the client I think. --- index.html | 13 ++++++++++--- index.js | 42 +++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/index.html b/index.html index 2b8c885..d7607a5 100644 --- a/index.html +++ b/index.html @@ -17,8 +17,8 @@

Tervetuloa Tänne :)

@@ -36,12 +36,19 @@