有没有人有任何示例代码演示在socket.io的
java客户端端接收消息?
另外,有没有从同一个socket.io java客户端发送文件/二进制/图片的例子? (基本上是来自java而不是javascript客户端的示例代码)
可以在这里获取android java客户端的版本(此版本声称它可以与socket.io 1.0及更高版本一起使用)(似乎是最新版本)
https://github.com/nkzawa/socket.io-client.java
目前只允许我初始化连接的示例代码,服务器能够获取我的传入连接事件,并且java socket.io客户端能够发出基本的发射消息.但是,没有简单的例子说明如何从服务器广播获取消息更新或从另一个网站用户发出消息更新.
示例代码仅供参考:
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
谢谢阅读