Trying using socket.io instead of websocket, because some device incompatibility
This commit is contained in:
@@ -60,7 +60,7 @@ public class PanTiltController {
|
||||
byte[] command = new byte[]{0, cameraid, 0, 0, 0, 0, 0};
|
||||
command[6] = Checksum(command); // add checksum
|
||||
command[0] = (byte) 0xFF; // add synchronization byte
|
||||
serialPort.writeBytes(command, command.length);
|
||||
if (serialPort!=null && serialPort.isOpen()) serialPort.writeBytes(command, command.length);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +74,7 @@ public class PanTiltController {
|
||||
byte[] command = new byte[]{0, cameraid, 0, 4, speed, 0, 0};
|
||||
command[6] = Checksum(command); // add checksum
|
||||
command[0] = (byte) 0xFF; // add synchronization byte
|
||||
serialPort.writeBytes(command, command.length);
|
||||
if (serialPort!=null && serialPort.isOpen()) serialPort.writeBytes(command, command.length);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +88,7 @@ public class PanTiltController {
|
||||
byte[] command = new byte[]{0, cameraid, 0, 2, speed, 0, 0};
|
||||
command[6] = Checksum(command); // add checksum
|
||||
command[0] = (byte) 0xFF; // add synchronization byte
|
||||
serialPort.writeBytes(command, command.length);
|
||||
if (serialPort!=null && serialPort.isOpen()) serialPort.writeBytes(command, command.length);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +102,7 @@ public class PanTiltController {
|
||||
byte[] command = new byte[]{0, cameraid, 0, 8, speed, 0, 0};
|
||||
command[6] = Checksum(command); // add checksum
|
||||
command[0] = (byte) 0xFF; // add synchronization byte
|
||||
serialPort.writeBytes(command, command.length);
|
||||
if (serialPort!=null && serialPort.isOpen()) serialPort.writeBytes(command, command.length);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +116,7 @@ public class PanTiltController {
|
||||
byte[] command = new byte[]{0, cameraid, 0, 16, speed, 0, 0};
|
||||
command[6] = Checksum(command); // add checksum
|
||||
command[0] = (byte) 0xFF; // add synchronization byte
|
||||
serialPort.writeBytes(command, command.length);
|
||||
if (serialPort!=null && serialPort.isOpen()) serialPort.writeBytes(command, command.length);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package Web;
|
||||
|
||||
import Other.SomeCodes;
|
||||
import com.corundumstudio.socketio.Configuration;
|
||||
import com.corundumstudio.socketio.SocketIOServer;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.UploadedFile;
|
||||
import io.javalin.util.FileUtil;
|
||||
@@ -26,6 +28,8 @@ public class WebServer {
|
||||
private @Getter @Setter String webpassword;
|
||||
private final Javalin app;
|
||||
private final Set<WsContext> connectedWebsocketClients = ConcurrentHashMap.newKeySet();
|
||||
private final SocketIOServer socketServer;
|
||||
private final Configuration socketConfig;
|
||||
public WebServer(WebsocketEvent event, String webusername, String webpassword){
|
||||
this.webusername = webusername;
|
||||
this.webpassword = webpassword;
|
||||
@@ -195,6 +199,27 @@ public class WebServer {
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
socketConfig = new Configuration();
|
||||
socketConfig.setHostname("0.0.0.0");
|
||||
socketConfig.setPort(3001);
|
||||
socketServer = new SocketIOServer(socketConfig);
|
||||
socketServer.addConnectListener(client -> {
|
||||
Logger.info("SocketIO connected, id {} from {}", client.getSessionId(), client.getRemoteAddress());
|
||||
});
|
||||
socketServer.addDisconnectListener(client -> {
|
||||
Logger.info("SocketIO disconnected, id {} from {}", client.getSessionId(), client.getRemoteAddress());
|
||||
});
|
||||
socketServer.addNamespace("/socketio").addEventListener("message", String.class, (client, data, ackSender) -> {
|
||||
//Logger.info("SocketIO message from namespace /socketio from {}: {}", client.getRemoteAddress(), data);
|
||||
WebsocketCommand command = gson.fromJson(data, WebsocketCommand.class);
|
||||
if (event!=null) {
|
||||
WebsocketReply reply = event.onWebsocketCommand(command);
|
||||
if (reply!=null) client.sendEvent("message", gson.toJson(reply));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,7 +227,11 @@ public class WebServer {
|
||||
* @param obj Object to send
|
||||
*/
|
||||
public void SendtoAll(Object obj){
|
||||
connectedWebsocketClients.forEach(wsContext -> wsContext.send(obj));
|
||||
|
||||
connectedWebsocketClients
|
||||
.stream()
|
||||
.filter(ws -> ws.session.isOpen())
|
||||
.forEach(ws -> ws.send(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,6 +244,8 @@ public class WebServer {
|
||||
connectedWebsocketClients.forEach(WsContext::closeSession);
|
||||
app.start(localip, port);
|
||||
Logger.info("Web server started at {}:{}", localip, port);
|
||||
socketServer.start();
|
||||
Logger.info("SocketIO server started at {}:{}", socketConfig.getHostname(), socketConfig.getPort());
|
||||
} catch (JavalinException e){
|
||||
Logger.error("Web server failed to start: {}", e.getMessage());
|
||||
}
|
||||
@@ -227,6 +258,8 @@ public class WebServer {
|
||||
try{
|
||||
app.stop();
|
||||
Logger.info("Web server stopped");
|
||||
socketServer.stop();
|
||||
Logger.info("SocketIO server stopped");
|
||||
} catch (JavalinException e){
|
||||
Logger.error("Web server failed to stop: {}", e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user