I'm implementing simple chat server that work fine in kind of browsers, but couldn't send message to specific user from android to other android device or android device to users which connected from browser, server said me user isn't connected, but in browser can detected correctly and sending message,i don't know whats problem, all of other feature work fine
<!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>
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 to redis");
redisClient.set(data.username, socket.id, function (err, res) {
redisClient.set(data.username, socket.id);
});
}
else {
log.info("deleted and saved to 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 to :), inja ro bebin ;)'});
} else {
log.info('mahdi' + ' NOT LOGin');
}
});
});
});
server.listen(port, function () {
console.log('Express and socket.io listening on port ' + port);
});
}
...
public class Application extends android.app.Application {
public static final boolean DEBUG = true;
public static Application application;
private static Context context;
public static String packageName;
public static Resources resources;
public static Socket CHAT_SOCKET;
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
}
@Override
public void onCreate() {
super.onCreate();
//@formatter:off
resources = this.getResources();
context = getApplicationContext();
packageName = getPackageName();
//@formatter:on
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;
}
}