7 changed files with 104 additions and 67 deletions
-
6src/main/java/com/dreamworks/boditech/controller/DeviceController.java
-
29src/main/java/com/dreamworks/boditech/controller/TmpController.java
-
2src/main/java/com/dreamworks/boditech/driver/Device.java
-
2src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardBox.java
-
37src/main/java/com/dreamworks/boditech/driver/consumable/CsmTestCardManager.java
-
45src/main/java/com/dreamworks/boditech/service/WebsocketServerService.java
-
50src/main/java/com/dreamworks/boditech/utils/MyWsServerConnection.java
@ -1,2 +1,29 @@ |
|||||
package com.dreamworks.boditech.controller;public class TmpController { |
|
||||
|
package com.dreamworks.boditech.controller; |
||||
|
import com.dreamworks.boditech.controller.entity.ApiResponse; |
||||
|
import com.dreamworks.boditech.service.WebsocketServerService; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Controller; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
import java.util.Map; |
||||
|
@Controller |
||||
|
public class TmpController extends BaseController { |
||||
|
@Resource |
||||
|
private WebsocketServerService wsServer; |
||||
|
|
||||
|
@ResponseBody |
||||
|
@PostMapping("/api/tmp/trigger-event") |
||||
|
public ApiResponse triggerEvent(@RequestBody Map<String,Object> params ) { |
||||
|
this.wsServer.send("{\"type\":\"event\",\"name\":\"" + params.get("name") + "\"}"); |
||||
|
return this.success(); |
||||
|
} |
||||
|
|
||||
|
@ResponseBody |
||||
|
@PostMapping("/api/tmp/trigger-error") |
||||
|
public ApiResponse triggerError(@RequestBody Map<String,Object> params ) { |
||||
|
this.wsServer.send("{\"type\":\"error\",\"name\":\"" + params.get("name") + "\",\"message\":\"DEMO_ERROR_MESSAGE\"}"); |
||||
|
return this.success(); |
||||
|
} |
||||
} |
} |
@ -1,44 +1,27 @@ |
|||||
package com.dreamworks.boditech.service; |
package com.dreamworks.boditech.service; |
||||
import jakarta.websocket.*; |
|
||||
import jakarta.websocket.server.ServerEndpoint; |
|
||||
import org.slf4j.Logger; |
|
||||
import org.slf4j.LoggerFactory; |
|
||||
|
import com.dreamworks.boditech.utils.MyWsServerConnection; |
||||
|
import jakarta.annotation.PostConstruct; |
||||
import org.springframework.stereotype.Service; |
import org.springframework.stereotype.Service; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
@Service |
@Service |
||||
@ServerEndpoint("/ws") |
|
||||
public class WebsocketServerService { |
public class WebsocketServerService { |
||||
private static final Logger LOG = LoggerFactory.getLogger(WebsocketServerService.class); |
|
||||
|
// connections |
||||
|
private List<MyWsServerConnection> connections; |
||||
|
|
||||
// only one session is all we need |
|
||||
private Session session; |
|
||||
|
|
||||
@OnOpen |
|
||||
public void onOpen(Session session) { |
|
||||
this.session = session; |
|
||||
LOG.info("ws server : on open"); |
|
||||
} |
|
||||
|
|
||||
@OnClose |
|
||||
public void onClose() { |
|
||||
LOG.info("ws server : on close"); |
|
||||
} |
|
||||
|
|
||||
@OnMessage |
|
||||
public void onMessage(String message) { |
|
||||
LOG.info("ws server : on message > " + message); |
|
||||
|
@PostConstruct |
||||
|
public void init() { |
||||
|
this.connections = new ArrayList<>(); |
||||
} |
} |
||||
|
|
||||
@OnError |
|
||||
public void onError(Session session, Throwable error) { |
|
||||
LOG.info("ws server : on error"); |
|
||||
|
public void appendConnection(MyWsServerConnection connection) { |
||||
|
this.connections.add(connection); |
||||
} |
} |
||||
|
|
||||
// send message to client |
// send message to client |
||||
public void send(Object message) { |
|
||||
try { |
|
||||
this.session.getBasicRemote().sendObject(message); |
|
||||
} catch (Exception e) { |
|
||||
LOG.error("ws server : send error", e); |
|
||||
|
public void send(String message) { |
||||
|
for (MyWsServerConnection connection : this.connections) { |
||||
|
connection.send(message); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
@ -0,0 +1,50 @@ |
|||||
|
package com.dreamworks.boditech.utils; |
||||
|
import com.dreamworks.boditech.service.WebsocketServerService; |
||||
|
import jakarta.websocket.*; |
||||
|
import jakarta.websocket.server.ServerEndpoint; |
||||
|
import org.springframework.beans.BeansException; |
||||
|
import org.springframework.context.ApplicationContext; |
||||
|
import org.springframework.context.ApplicationContextAware; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
@Component |
||||
|
@ServerEndpoint("/ws") |
||||
|
public class MyWsServerConnection implements ApplicationContextAware { |
||||
|
// spring application context |
||||
|
private static ApplicationContext applicationContext; |
||||
|
// websocket server service |
||||
|
private WebsocketServerService wsServer; |
||||
|
|
||||
|
@Override |
||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
||||
|
MyWsServerConnection.applicationContext = applicationContext; |
||||
|
} |
||||
|
|
||||
|
// only one session is all we need |
||||
|
private Session session; |
||||
|
|
||||
|
@OnOpen |
||||
|
public void onOpen(Session session) { |
||||
|
this.session = session; |
||||
|
this.wsServer = MyWsServerConnection.applicationContext.getBean(WebsocketServerService.class); |
||||
|
this.wsServer.appendConnection(this); |
||||
|
} |
||||
|
|
||||
|
@OnClose |
||||
|
public void onClose() { |
||||
|
System.out.println("ws server : on close"); |
||||
|
} |
||||
|
|
||||
|
@OnMessage |
||||
|
public void onMessage(String message) { |
||||
|
System.out.println("ws server : on message"); |
||||
|
} |
||||
|
|
||||
|
@OnError |
||||
|
public void onError(Session session, Throwable error) { |
||||
|
System.out.println("ws server : on error"); |
||||
|
} |
||||
|
|
||||
|
public void send( String text ) { |
||||
|
this.session.getAsyncRemote().sendText(text); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue