尝试通过Socket.IO获取Android客户端(在智能手机上)与Java服务器通信。他们设法连接一次,但我从未设法重现连接。
任何帮助将不胜感激!
(我已经验证了主机服务器的IP地址,并尝试了多个端口。)
Android客户端:
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends AppCompatActivity {
private Socket mSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mSocket = IO.socket("http://192.168.0.15:9092");
} catch (URISyntaxException e) {
e.printStackTrace();
}
mSocket.connect();
}
Android权限包含:
<uses-permission android:name="android.permission.INTERNET" />
Java服务器:
import com.corundumstudio.socketio.*;
public class IOServer {
final SocketIOServer server;
public IOServer()
{
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);
server = new SocketIOServer(config);
server.addConnectListener(new ConnectListener(){
@Override
public void onConnect(SocketIOClient client) {
System.out.println("CONNECTION!");
System.out.println(client.toString());
}
});
server.start();
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
server.stop();
}
}
(客户端使用:io.socket:socket.io-client:0.8.3,服务器使用:com.corundumstudio.socketio)