node.js - Atuthentication with socket io in android for chatting app -
i developing 1 one chatting app socket.io in android. can send & receive messages single chat room. following this tutorial. apps chat module looks that. now, want sent & receive message single user.
during development, observed each , every socket connection , socket.io gives client new id looks :
/#iyq7lakzlclf7g3daaaa
for reasons can't track specific user send message.
qs 1. have connect database storing user credential send message specific user & send offline message him/her? chatting feature additional function in android app. app uses token based authentication.
i novice in node js & socket.io. give me architectural guideline how solve this. tia.
my app.js code :
var express = require('express'); var app = express(); var server = require('http').createserver(app); var io = require('socket.io')(server); var port = process.env.port || 3000; server.listen(port, function () { console.log('server listening @ port %d', port); }); // routing app.use(express.static(__dirname + '/public')); // chatroom var numusers = 0; var clients = []; io.on('connection', function (socket) { var addeduser = false; clients.push(socket); console.log('one user connected: user name: ' +socket.username +"------ id : >> "+ socket.id); console.log('total user list:' + clients); socket.on('connect', function (data) { // tell client execute 'new message' socket.broadcast.emit('connect', { username: socket.username, numusers: numusers, socket_id:socket.id }); }); // when client emits 'new message', listens , executes socket.on('new message', function (data) { // tell client execute 'new message' socket.broadcast.emit('new message', { username: socket.username, message: data }); }); // when client emits 'add user', listens , executes socket.on('add user', function (username) { if (addeduser) return; // store username in socket session client socket.username = username; ++numusers; addeduser = true; socket.emit('login', { numusers: numusers }); // echo globally (all clients) person has connected socket.broadcast.emit('user joined', { username: socket.username, numusers: numusers, socket_id:socket.id }); }); // when client emits 'typing', broadcast others socket.on('typing', function () { socket.broadcast.emit('typing', { username: socket.username }); }); // when client emits 'stop typing', broadcast others socket.on('stop typing', function () { socket.broadcast.emit('stop typing', { username: socket.username }); }); // when want send message specific user socket.on('say someone', function (id, msg) { socket.broadcast.to(id).emit('say someone', { username: socket.username, id:id, message: msg }); }); // when user disconnects.. perform socket.on('disconnect', function () { clients.splice(clients.indexof(socket), 1); console.log('disconnected... ' + socket.id); if (addeduser) { --numusers; // echo globally client has left socket.broadcast.emit('user left', { username: socket.username, numusers: numusers }); } }); });
there problem implementation of socket.on('say someone', function (id, msg)
please see link below
Comments
Post a Comment