Hi everyone ! I try to send some data to server, but there is trouble with it :(
Here is server-side code
` import validator from 'validator';
import socketioJwt from 'socketio-jwt';
import * as funs from './user.funs';
const secret = process.env.SECRET;
export default function socketInit(server) {
const socketio = require('socket.io')(server);
socketio.use(socketioJwt.authorize({
secret,
decodedPropertyName: 'user',
handshake: true
}));
socketio.on('connection', function (socket) {
const {phone} = socket.user,
executor = !!socket.user.executor,
customer = !!socket.user.customer;
console.log('user with phonenumber - ' + phone + ' connected');
socket.on('',(data)=>{
console.log("************");
console.log(data);
});
// when the user toggles online.. perform this
socket.on('online', ()=>{
funs.userOnline(phone, socket, socketio)
.then(()=>{
socket.emit('online', {msg: 'success'});
socketio.emit('user online', {msg: 'a new user online'});
})
.catch((err)=>{
console.log(err);
socket.emit('error',{msg:'failed to set online'});
socket.disconnect(true);
})
});
// when executor wants to
socket.on('order', (data)=>{
console.log("*************");
console.log(data);
data = JSON.parse(data);
const {
coordinates, address, office,
size, comments
} = data;
socket.disconnect(true);
});
socket.on('ready', (data)=>{
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
console.log('user with phonenumber - ' + phone + ' disconnected');
funs.userOffline(phone)
.then(()=>{
socket.emit('offline', { msg: 'success' });
socket.disconnect(true);
})
.catch((err)=>{
socket.emit('error',{msg:'failed to set offline'});
socket.disconnect(true);
});
});
});
}`
And here is code from android
`try {
IO.Options opts = new IO.Options();
opts.query = "token=" + PreferenceManager.getToken(CreateOrder.this);
socket = IO.socket(Constants.sBaseUrl + "socket.io/", opts);
} catch (URISyntaxException e) {
e.printStackTrace();
}
socket.connect();
socket.emit("order", "something");`
Can you tell me where is the problem ?
I am facing a similar problem. I am able to connect to a Java Netty Socket IO server (https://github.com/mrniko/netty-socketio) but the Emit event from Android client is not being received by the server. No errors in LogCat or the server seen.
@hova4 In your case, you should do the emit() after the EVENT_CONNECT handler is triggered. You are trying to emit before the connection is completed. Please try that.
`try {
socket = IO.socket(Constants.sBaseUrl + "socket.io/?token="
+ PreferenceManager.getToken(getApplicationContext()));
} catch (URISyntaxException e) {
e.printStackTrace();
}
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("foo", "hi");
// socket.disconnect();
}
}).on("event", new Emitter.Listener() {
@Override
public void call(Object... args) {
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
}
});
socket.connect();
socket.emit("order", "something");
}`
@adarshu Hi dear friend! You said to try emit() after connecting, you mean in this way ?
P.S in attach photo logs from debugging
@hova4 can you post your code in a gist or edit the formatting so everything is in the code-block? I may be able to help!
@lentusignavus Hi dear friend ! Below is 2 gist links.
Node.js code
Java code
It doesn't work for me too :(
Hey guys,
I finally got the Android side working. Issue was that Node server side was unable to deserialize the JSONObject since I was sending a string (doh!). So the emit was working all along!
Here is my Android client code:
public void setupNotifyListener() {
try {
Log.d(TAG, "=== setup notifylistener...");
IO.Options ioopts = new IO.Options();
ioopts.timeout = 5000;
ioopts.transports = trx;
String ioconnstr = AppOptions.httpuse + AppOptions.cloudNotifyServerIp + ":" + AppOptions.cloudNotifyListenPort;
Log.d(TAG, "=== connect to notify server at: " + ioconnstr);
socketioclient = IO.socket(ioconnstr, ioopts);
socketioclient.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
public void call(Object... args) {
Log.d(TAG, "=== notifylistener connected!");
JSONObject obj = new JSONObject();
obj.put("name", "Test");
Log.d(TAG, "emitting:" + obj.toString());
socket.emit(stype, obj);
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
public void call(Object... args) {
Log.d(TAG, "=== notifylistener disconnected!");
}
}).on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
public void call(Object... args) {
Log.d(TAG, "=== notifylistener reconnecting: ");
}
}).on(Socket.EVENT_ERROR, new Emitter.Listener() {
public void call(Object... args) {
Log.d(TAG, "=== notifylistener error: ");
}
}).on("trigger", new Emitter.Listener() {
public void call(Object... args) {
try {
JSONObject data = (JSONObject) args[0];
Log.d(TAG, "\n=== Notif event received: " + data.toString());
Log.d(TAG, "\n\n<-- GOT new request to connect from CloudServer");
} catch (JSONException e) {
Log.d(TAG, "=== notifylistener error: ", e);
}
}
});
socketioclient.connect();
} catch (URISyntaxException e) {
Log.d(TAG, "=== notifylistener error: ", e);
}
}
@adarshu Hi dear friend ! Thanks for your response !
I'm try to do with your example, but know i'm get this error :
io.socket.engineio.client.EngineIOException: websocket error
I am using this libraries :
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
Can you explain what error is this ?
And what means this lines in your code ?
ioopts.transports = trx;
String ioconnstr = AppOptions.httpuse + AppOptions.cloudNotifyServerIp + ":" + AppOptions.cloudNotifyListenPort;
Thanks a lot !
You would need to post the full stack trace. Can't tell from just that one line. If you imported latest version of client library and use the latest server library, it should work.
@adarshu In android client
compile('io.socket:socket.io-client:0.8.3') {
exclude group: 'org.json', module: 'json'
}
in server side 1.7.3 version.
full error message attached in picture.
Thanks for your time!
I'm gonna have to say it's likely an SSL issue. Maybe try using a plain socket IO server instead of an SSL one? Are you perhaps trying to use SSL to connect to non-SSL server? It's also possible the certificate on the server is expired, self-signed, or missing an intermediary certificate.
Most helpful comment
Hey guys,
I finally got the Android side working. Issue was that Node server side was unable to deserialize the JSONObject since I was sending a string (doh!). So the emit was working all along!
Here is my Android client code: