Answer the question
In order to leave comments, you need to log in
Why is text not being sent from the client to the WebSocket server?
Hello, I am writing a network application that communicates with the server via WebSocket.
The client runs on Android
The server part is running on my computer) it was also written in Java.
I used the same library
for the client and the server.
The server implemented it like this:
import java.net.InetSocketAddress;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
public class SimpleServer extends WebSocketServer {
public SimpleServer(InetSocketAddress address) {
super(address);
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
System.out.println("new connection to " + conn.getRemoteSocketAddress());
conn.send("Привет!");
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
System.out.println("closed " + conn.getRemoteSocketAddress() + " with exit code " + code + " additional info: " + reason);
}
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("received message from " + conn.getRemoteSocketAddress() + ": " + message);
}
@Override
public void onError(WebSocket conn, Exception ex) {
System.err.println("an error occured on connection " + conn.getRemoteSocketAddress() + ":" + ex);
}
public static void main(String[] args) {
String host = "172.16.16.11";
int port = 1122;
WebSocketServer server = new SimpleServer(new InetSocketAddress(host, port));
server.run();
}
}
package ru.domen.myapplication;
import android.util.Log;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.drafts.Draft;
import java.net.URI;
class SoketWS extends WebSocketClient{
public SoketWS(URI serverURI, Draft draft) {
super(serverURI);
}
public SoketWS(URI serverURI) {
super(serverURI);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.d("test","есть контакт");
}
@Override
public void onMessage(String message) {
Log.d("test","Server: "+ message);
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.d("test","Close");
}
@Override
public void onError(Exception ex) {
}
}
try{
WebSocketClient client = new SoketWS(new URI("ws://172.16.16.11:1122"), new Draft_10());
client.connect();
client.send("Привет");
}catch (URISyntaxException e){
Log.d("test", "Не удалось подключиться к серверу");
}
catch (RuntimeException e){
Log.d("test", "Исключение какое то с подключением");
}
07-31 10:43:13.008 19689-19689/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ru.domen.myapplication, PID: 19689
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.domen.myapplication/ru.domen.myapplication.MainActivity}: org.java_websocket.exceptions.WebsocketNotConnectedException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.java_websocket.exceptions.WebsocketNotConnectedException
at org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:566)
at org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:543)
at org.java_websocket.client.WebSocketClient.send(WebSocketClient.java:171)
at ru.domen.myapplication.MainActivity.onCreate(MainActivity.java:167)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
client.send("Привет");
Answer the question
In order to leave comments, you need to log in
I solved the problem by changing the library.
I found the answer here: autobahn.ws/android/gettingstarted.html
The only thing is, if you include the full list of libraries:
I got an error when building the project, to implement the client part for android, it's enough to leave AutobahnAndroid
in the source, there is also an example client.
When writing a server left this library
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question