i'm implementing simple chat server work fine in kind of browsers, couldn't send message specific user android other android device or android device users connected browser, server said me user isn't connected, in browser can detected correctly , sending message,i don't know whats problem, i'm using nkzawa/socket.io-android-chat library on android , of other feature work fine
html test , sending message specific user:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title> document </title> </meta> <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#login').click(function () { socket.emit('login', {username: 'a', password: 'a'}); }); $('#mesage').click(function () { socket.emit('requestmoney', 'mahdi'); }); }); </script> <script> var socket = new io.connect('http://192.168.1.35:3000', { port : 3000, transports: ['websocket'] }); socket.on('connect', function () { console.log('connected!'); }); socket.on('message', function (message) { console.log("hello ::::::::" + message); }); socket.on('success', function (data) { console.log(data); }); </script> </head> <body> <h3 id="login">login</h3> <h3 id="mesage">mesage</h3> </body> </html>
my simplified nodejs server:
var socket = require('socket.io'), express = require('express'), app = express(), server = require('http').createserver(app), io = socket.listen(server), port = process.env.port || 3000, mysql = require('mysql'), multer = require('multer'), uuid = require('node-uuid'), datetime = require('node-datetime'), moment = require('moment'), bcrypt = require('bcrypt'), async = require('async'), request = require('request'), redis = require("redis"), redisclient = redis.createclient(), log = require('log4node'); var io_redis = require('socket.io-redis'); io.adapter(io_redis({host: 'localhost', port: 6379})); require('sticky-socket-cluster/replace-console')(); require('sticky-socket-cluster')(options, start); function start(port) { io.sockets.on('connection', function (socket) { socket.on('login', function (data) { console.log(data.username); login(data.username, data.password, function (success, value) { if (success) { redisclient.exists(data.username, function (err, doesexist) { if (err) return; if (!doesexist) { log.info("saved redis"); redisclient.set(data.username, socket.id, function (err, res) { redisclient.set(data.username, socket.id); }); } else { log.info("deleted , saved redis"); redisclient.del(data.username); redisclient.set(data.username, socket.id, function (err, res) { redisclient.set(data.username, socket.id); }); } }); socket.emit('login', {result: true, id: value}); } else { log.info("fail login"); socket.emit('login', {result: false}); } }); }); socket.on('requestmoney', function (data) { redisclient.get('mahdi', function (err, socketid) { if (io.sockets.connected[socketid]) { log.info('mahdi' + ' sent'); io.sockets.connected[socketid].emit('message', {username: 'hey :), inja ro bebin ;)'}); } else { log.info('mahdi' + ' not login'); } }); }); }); server.listen(port, function () { console.log('express , socket.io listening on port ' + port); }); } ...
android socket settings , options, use namespace of library:
public class application extends android.app.application { ... public static socket chat_socket; @override protected void attachbasecontext(context base) { super.attachbasecontext(base); } @override public void oncreate() { super.oncreate(); ... io.options opts = new io.options(); opts.forcenew = true; opts.reconnection = true; try { chat_socket = io.socket(clientsettings.getchataddress(), opts); } catch (urisyntaxexception e) { e.printstacktrace(); log.e("socket.io ", e.getmessage()); } } public static context getcontext() { return context; } public socket getsocket() { return chat_socket; } }
Comments
Post a Comment