如何使用Socket.IO-client Java从Android向服务器发送字符串消息?
我使用图书馆 我有android客户端和Java服务器 服务器代码:如何使用Socket.IO-client Java从Android向服务器发送字符串消息?,java,android,sockets,socket.io,Java,Android,Sockets,Socket.io,我使用图书馆 我有android客户端和Java服务器 服务器代码: public class Server { public static void main(String[] args) throws IOException { System.out.println("Welcome to Server side"); BufferedReader in = null; PrintWriter out = null; ServerSocket servers
public class Server {
public static void main(String[] args) throws IOException {
System.out.println("Welcome to Server side");
BufferedReader in = null;
PrintWriter out = null;
ServerSocket servers = null;
Socket fromclient = null;
// create server socket
try {
servers = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Couldn't listen to port 4444");
System.exit(-1);
}
try {
System.out.print("Waiting for a client...");
fromclient = servers.accept();
System.out.println("Client connected");
} catch (IOException e) {
System.out.println("Can't accept");
System.exit(-1);
}
in = new BufferedReader(new
InputStreamReader(fromclient.getInputStream()));
out = new PrintWriter(fromclient.getOutputStream(), true);
String input, output;
System.out.println("Wait for messages");
while ((input = in.readLine()) != null) {
if (input.equalsIgnoreCase("exit")) break;
out.println("S ::: " + input);
System.out.println(input);
}
out.close();
in.close();
fromclient.close();
servers.close();
}
}
客户:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Socket mSocket = null;
private Button button;
{
try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
mSocket.connect();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button){
mSocket.emit("new message", "Some message");
}
}
}
如果我尝试连接到服务器,我会得到以下结果:
Welcome to Server side
Waiting for a client...Client connected
Wait for messages
GET /socket.io/?EIO=3&transport=polling HTTP/1.1
User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.7; Android SDK built for x86 Build/GINGERBREAD)
Host: 10.0.2.2:4444
Connection: Keep-Alive
Accept-Encoding: gzip
但当我试图发送字符串消息时
mSocket.emit("new message", "Some message");
什么也没发生。发送错误或服务器错误?要正确初始化socket.io客户端,您需要执行以下操作:
Socket _socket = IO.socket(SERVER_URI);
_socket.on(Socket.EVENT_CONNECT, _onConnectionListener)
.on(Socket.EVENT_DISCONNECT, _onDisconnectListener)
.on(Socket.EVENT_CONNECT_ERROR, _onConnectionErrorListener)
.on(Socket.EVENT_MESSAGE, _onMessageReceivedListener)
.on(CHAT_MESSAGE_PUBLISH, _onMessageReceivedListener);
每个侦听器都在事件发生时运行,例如:
private Emitter.Listener _onMessageReceivedListener = new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONArray jsonArray = (JSONArray) args[0];
if (jsonArray != null && jsonArray.length() > 0) {
Gson gson = new Gson();
List<MessageModel> historyList = gson.fromJson(jsonArray.toString(), listType);
loadReceivedMessage(historyList);
chatLoging(jsonArray.toString());
}
}
};
private-Emitter.Listener _-onMessageReceivedListener=new-Emitter.Listener(){
@凌驾
公共无效调用(对象…参数){
JSONArray JSONArray=(JSONArray)args[0];
if(jsonArray!=null&&jsonArray.length()>0){
Gson Gson=新的Gson();
List historyList=gson.fromJson(jsonArray.toString(),listType);
loadReceivedMessage(历史列表);
聊天日志(jsonArray.toString());
}
}
};
当您收到来自服务器的新消息时,将触发
call()
方法。谢谢您将文档复制并粘贴到此处