Closed yangman92 closed 3 months ago
In addition, i also code a server. I used two as a server and a client. it worked , both successded and can sent message to each.
But i need to connect the tool that is our device.
server coding
public class ServerSocket extends WebSocketServer {
private static final String TAG = "Socket - ServerSocket";
private OnServerSocketListener onServerSocketListener;
public void setOnServerSocketListener(OnServerSocketListener onServerSocketListener) {
this.onServerSocketListener = onServerSocketListener;
}
public ServerSocket(int port) throws UnknownHostException {
super(new InetSocketAddress(port));
}
public ServerSocket(InetSocketAddress address) {
super(address);
LogUtils.i(TAG, "ServerSocket" + address.getHostString());
}
public ServerSocket(int port, Draft_6455 draft) {
super(new InetSocketAddress(port), Collections.<Draft>singletonList(draft));
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
LogUtils.i(TAG, "onOpen 加入连接" + conn.getRemoteSocketAddress().getAddress().getHostAddress());
if (onServerSocketListener != null) {
onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 连接到服务端");
}
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
LogUtils.i(TAG, "onClose 断开连接" + conn.getRemoteSocketAddress().getAddress().getHostAddress());
if (onServerSocketListener != null) {
onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 断开链接");
}
}
@Override
public void onMessage(WebSocket conn, String message) {
LogUtils.i(TAG, "onMessage 收到信息" + conn.getRemoteSocketAddress().getAddress().getHostAddress() + message);
if (onServerSocketListener != null) {
onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 发送了消息 : " + message);
}
}
@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
LogUtils.i(TAG, "onError " + ex.getMessage());
if (onServerSocketListener != null) {
onServerSocketListener.onLogging("服务端异常" + ex.getMessage());
}
}
@Override
public void onStart() {
LogUtils.i(TAG, "onStart " + getAddress().toString());
if (onServerSocketListener != null) {
onServerSocketListener.onLogging("服务端启动 " + getAddress().toString());
}
}
}
where i used to
server = new ServerSocket(new InetSocketAddress(3333));
server.start();
I dont see any real cause in the provided code. But since the server side is not provided, I cannot help and guess.
Closing this issue therefore as cannot reproduce.
I used a tool as a server. and i used my phone to connect with. The problem now is a the server was connected when i used my phone to connect.
Client coding blow:
use location
The result from the picture show that there is a client has successfuly connected, but i didn't see any error from my client.
Can you give me an idea about this problem? Thanks a lot.