I'm new to socket programming and in a project i'm using Socket.IO-client Java to create socket connection between android device and a server . server side developers asked me to connect to the server and then join in a specific room/channel . I'm OK with the first part but how can i join in a room over socket ??
my code is as below :
private void ConnectAndJoin() {
Log.e("starting:","starting!");
final Socket socket;
try {
socket = IO.socket("http://192.168.1.6:5000");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("foo", "hi");
Log.e("status:","connected- sent");
socket.
}
}).on("event", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("status:","event2");
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("status:","disconnected");
}
});
socket.connect();
} catch (URISyntaxException e) {
Log.e("status:","done with error");
}
Please give me a help!
You can't join a room with socket io from the client side . it should happens in the server side.
socket.emit('join-room' , roomName or RoomId)
to tackle this you need to emit a socket with the room you want to join as data , and in the server you listen to this socket and call socket.join() with the name or the id of the room that has been sent.
socket.on("join-room", new Emitter.Listener() {
@Override
public void call(Object... args) {
room = args[0];
socket.join(room);
}
});
warning : I do not know java so for java devs if you see a syntax mistake you can comment i'll update it
hope it helps!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments