public class WebsocketService2 extends Service {
private static final String TAG = com.example.icommunity.websocket.WebsocketService2.class.getSimpleName();
private Subscription subscription;
private WebSocket mwebsocket;
//public static Queue<Float> queue = new LinkedList<>();
public static Queue<String> queue = new LinkedList<>();
public WebsocketService2() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate executed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand executed");
setForegroundService();
initWebSocket();
//test();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
if (subscription!=null&subscription.isUnsubscribed())
subscription.unsubscribe();
Log.d(TAG, "onDestroy executed");
}
}
private void initWebSocket(){
Config config = new Config.Builder()
.setShowLog(true) //show log
.setClient(new OkHttpClient.Builder()
.pingInterval(3, TimeUnit.SECONDS) // 设置心跳间隔,这个是3秒检测一次
.build()) //if you want to set your okhttpClient
.setShowLog(true, "your logTag")
.setReconnectInterval(2, TimeUnit.SECONDS) //set reconnect interval
//.setSSLSocketFactory(yourSSlSocketFactory, yourX509TrustManager) // wss support
.build();
RxWebSocket.setConfig(config);
subscription=RxWebSocket.get(ApiConstants.WEBSOCKET_URL)
.subscribe(new WebSocketSubscriber() {
@Override
public void onOpen(@NonNull WebSocket webSocket) {
mwebsocket = webSocket;
mwebsocket.send("hello");
String text = "{\"pack\":[{\"soilHumidityPacketId\":\"null\",\"eid\":\"6a414058\",\"dataType\":\"00\",\"soilHumidity\":\"5.0\",\"commitTimw\":\"1619437839592\"},{\"probeTempPacketId\":\"null\",\"eid\":\"a44a7184\",\"dataType\":\"01\",\"probeTemp\":\"22.7\",\"commitTime\":\"1619437839592\"},{\"probeHumidityPacketId\":\"null\",\"eid\":\"7580ecf1\",\"dataType\":\"02\",\"probeHumidity\":\"55.3\",\"commitTime\":\"1619437839595\"},{\"airTemPacketId\":\"null\",\"eid\":\"2b3eaba2\",\"dataType\":\"03\",\"airTemp\":\"21.6\",\"commitTime\":\"1619437839597\"},{\"airHumidityPacketId\":\"null\",\"eid\":\"db664217\",\"dataType\":\"04\",\"airHumidity\":\"64.4\",\"commitTime\":\"1619437839600\"},{\"sunlightPacketId\":\"null\",\"eid\":\"0cc36928\",\"dataType\":\"05\",\"intensity\":\"16.0\",\"commitTime\":\"1619437839602\"}]}";
// Log.d(TAG, "onOpen1:");
// Log.d("TAG", "返回数据"+ ":" + text);
// parseJSONWITHJSONObject(text);
// JsonParser parse = new JsonParser();
// JsonObject json = (JsonObject) parse.parse(text);
}
@Override
public void onMessage(@NonNull String text) {
// String flag = text.substring(1,5);
// if ("pack".equals(flag)){
// Log.d("TAG", "走到了吗 ");
// }
// parseJSONWithGSON(text);
// int time = (int) (System.currentTimeMillis()/1000);
// String flag = text.substring(2 , 6);
// String msg = null;
// if ("pack".equals(flag)){
// msg = text.substring(8 , text.length() - 1);
// ObjectMapper mapper = new ObjectMapper();
// JSONArray array = null;
// try {
// array = new JSONArray(msg);
// }catch (JSONException e){
// e.printStackTrace();
// }
//
// for (int i =0; i<array.length() ; i++){
// JSONObject object = null;
// if (i <12){
// try{
// object = array.getJSONObject(i);
// int type = object.getInt("dataType");
// switch (type){
// case 00:
// SoilPacket soilPacket = mapper.readValue(object.toString(),SoilPacket.class);
// break;
// case 01:
// WaterPacket waterPacket = mapper.readValue(object.toString(),WaterPacket.class);
// break;
// case 03:
// AirPacket airPacket = mapper.readValue(object.toString(),AirPacket.class);
// break;
// case 05:
// SunlightPacket sunlightPacket = mapper.readValue(object.toString(),SunlightPacket.class);
// break;
// default:
// break;
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }else {
// try{
// object = array.getJSONObject(i);
// }catch (Exception e){
// e.printStackTrace();
// }
// }
// }
// }
// if ("list".equals(flag)){
// msg = text.substring(8,text.length() - 1);
// ObjectMapper mapper = new ObjectMapper();
// JSONArray array = null;
// try{
// array = new JSONArray(msg);
// }catch (JSONException e){
// e.printStackTrace();
// }
// }
// Intent intent = new Intent();
// intent.putExtra("current",text);
// intent.setAction("com.example.icommunity.TEMPERATURE_BROADCAST");
// sendBroadcast(intent);
}
@Override
protected void onReconnect() {
Log.d(TAG, "重连");
}
@Override
protected void onClose() {
Log.d(TAG, "onClose: ");
}
});
}
注释的都是我试过的方法,但都有问题