Does anyone have any sample code which demonstrates receiving messages on the java client side for socket.io?
Also, is there any example on sending a file/binary/picture over from the same socket.io java client? (basically sample code from java instead of javascript client)
The version of android java client can be acquired here (this version claim that it can be used with socket.io 1.0 and later) (seems to be the most updated version) https://github.com/nkzawa/socket.io-client.java
Currently the sample code which only allows me to initialize a connection, the server is able to get my incoming connection event and the java socket.io client is able to send out a basic emit message. However, there is no plain simple examples on how to acquire a message update from a server broadcast or emits from another website user.
Sample code just for reference:
package com.sample.d10132014a;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.github.nkzawa.socketio.client.*; // java socket io client
import com.github.nkzawa.socketio.client.Socket;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.engineio.client.*; // java engine io client
import com.github.nkzawa.engineio.client.transports.*;
public class MainActivity extends Activity {
public static String internalPath; // internal storage path
public static String fileName; // the file name
private Socket socket; // socket object
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
socket = IO.socket("http://YOURSERVERIP:3000");
socket.connect(); // initiate connection to socket server
socket.emit("chat message", "From Android to server: 1st outgoing message");
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener()
{
@Override
public void call(Object... args) {
Log.d("socketio", "socket connected");
socket.emit("chat message", "even connect: message sent from android to socketio server");
//socket.disconnect(); // why is there a disconnect here?
}
}).on("chat message", new Emitter.Listener() {
@Override
public void call(Object... arg0) {
// TODO Auto-generated method stub
JSONObject obj = (JSONObject)arg0[0];
Log.d("socketio", "message back: "+obj.toString());
Log.d("socketio", "incomming chat message: " + obj.toString() + arg0 + arg0[0] + arg0[1]); // trying to test which variable holds the message
}
}).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... arg0) {
// TODO Auto-generated method stub
Log.d("socketio", "socket event message" + arg0);
socket.emit("chat message", "android to server from event message");
}
});
// 2nd segment test without connecting to 1 long method
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener()
{
@Override
public void call(Object... arg0) {
// TODO Auto-generated method stub
Log.d("socketio", "socket event connect error");
socket.emit("chat message", "android to server: socket event connect error");
}
});
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override
public void call(Object... arg0) {
// TODO Auto-generated method stub
Log.d("socketio", "socket event message" + arg0);
socket.emit("chat message", "android to server from event message");
}
});
setContentView(R.layout.activity_main);
} // ending onCreate method
} // ending class
Thanks for reading