-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathappBackend.java
More file actions
70 lines (56 loc) · 2.2 KB
/
Copy pathappBackend.java
File metadata and controls
70 lines (56 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.example.draftfinal;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "WebSocketApp";
private static final String WEBSOCKET_URL = "ws://192.168.247.252:8080"; // Replace with your server IP
private WebSocket webSocket;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// Initialize WebSocket connection
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(WEBSOCKET_URL).build();
webSocket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
Log.d(TAG, "WebSocket connected");
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
Log.d(TAG, "Message received: " + text);
// Update the UI with the received message
runOnUiThread(() -> textView.setText(text));
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, response);
Log.e(TAG, "WebSocket error", t);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
Log.d(TAG, "WebSocket closed: " + reason);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (webSocket != null) {
webSocket.close(1000, "App closed");
}
}
}