目錄
流程圖
可運行的websock代碼
收消息
/**
* 收到客戶端消息後調用的方法
*
* @param message 客戶端發送過來的消息*/
@OnMessage
public void onMessage(String message, Session session) throws IOException {
log.info("收到來自窗口"+sid+"的叫號信息:"+message);
//生產目標消息然後回調發送消息
this.sendMessage("接收到"+message+"。 請訪問接口"+"'"+"MessageToCaller"+"'"+"獲取當前窗口的排隊信息");
}
發消息
public static void sendInfo(String message,@PathParam("sid") String sid) throws IOException {
log.info("推送消息到窗口"+sid+",推送內容:"+message);
for (WebSocketServer item : webSocketSet) {
try {
//這裏可以設定只推送給這個sid的,爲null則全部推送
if(sid==null) {
item.sendMessage(message);
}else if(item.sid.equals(sid)){
item.sendMessage(message);
}
} catch (IOException e) {
continue;
}
}
}
D:\wsy\下載\websocket\springboot + netty + websocket\pxm6666-springboot-websocket-master\springboot-websocket
多人聊天、單人聊天
D:\wsy\下載\springboot整合\csdn下載的多人聊天和單人聊天\wbsocket
接收到客戶端的json需要經過處理,目前可應答
D:\wsy\project\排隊機\4.實現\code\v1.0\wbsocket_沒有加生產者,可應答.zip
D:\wsy\下載\springboot整合\csdn下載的多人聊天和單人聊天\wbsocket_yuan 整合rocketmq
參考網址:
https://blog.csdn.net/qq_35387940/article/details/93483678
https://www.bilibili.com/video/av71654125?p=18 黑馬rocket教程
websocket在接口中被調用。
@Controller
@RequestMapping("/checkcenter")
public class WebSocketController {
//推送數據接口
@ResponseBody
@RequestMapping("/socket/push/{cid}")
public String pushToWeb(@PathVariable String cid,String message) {
try {
WebSocketServer.sendInfo(message,cid);
} catch (IOException e) {
e.printStackTrace();
return "failure";
}
return "success";
}
}
評論:實際項目中,服務器和客戶端交互,2種方案:
A:客戶端訪問接口,是訪問接口的數據,接口的數據來自mq,就像是來自數據庫一樣的道理。
B: 服務器訪問接口,把數據發送給客戶端。爲什麼不直接發?爲了系統中其他角色也能調用這個接口。就像是一個項目組中合作的成員,各自做自己的事情,有些資源需要共享
項目地址:
D:\wsy\下載\springboot整合\beyondli71-websocket-demo-master\websocket-demo
springboot整合Rockmq
消費者:
@Slf4j
@Component
@RocketMQMessageListener(topic = "springboot-mq",consumerGroup = "springboot-mq-consumer-1")
public class Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
log.info("Receive message:"+message);
}
pom.xml
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>${rocketmq-spring-boot-starter-version}</version>
</dependency>
地址:D:\wsy\下載\黑馬\RocketMQ\代碼\springboot-rocketmq-consumer
生產者:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MQSpringBootApplication.class})
public class ProducerTest {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Test
public void test1(){
rocketMQTemplate.convertAndSend("springboot-mq","hello springboot rocketmq");
}
}
地址:D:\wsy\下載\黑馬\RocketMQ1\代碼\springboot-rocketmq-producer
在websocket中調用MQ的producer——sptingboot整合ms的項目
發消息
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage textMessage) throws Exception {
Long uid = (Long) session.getAttributes().get("uid");
JsonNode jsonNode = MAPPER.readTree(textMessage.getPayload());
Long toId = jsonNode.get("toId").asLong();
String msg = jsonNode.get("msg").asText();
// 將消息保存到MongoDB
// message = this.messageDAO.saveMessage(message);
String msgJson = MAPPER.writeValueAsString(message);
// 判斷to用戶是否在線
WebSocketSession toSession = SESSIONS.get(toId);
if (toSession != null && toSession.isOpen()) {
//TODO 具體格式需要和前端對接
toSession.sendMessage(new TextMessage("你好"));
// 更新消息狀態爲已讀
// this.messageDAO.updateMessageState(message.getId(), 2);
}else{
// 該用戶可能下線,可能在其他的節點中,發送消息到MQ系統
// 需求:添加tag,便於消費者對消息的篩選
this.rocketMQTemplate.convertAndSend("haoke-im-send-message-topic:SEND_MSG", msgJson);
}
}
收消息
@Override
public void onMessage(String msg) {
try {
JsonNode jsonNode = MAPPER.readTree(msg);
long toId = jsonNode.get("to").get("id").longValue();
// 判斷to用戶是否在線
WebSocketSession toSession = SESSIONS.get(toId);
if (toSession != null && toSession.isOpen()) {
//TODO 具體格式需要和前端對接
toSession.sendMessage(new TextMessage(msg));
// 更新消息狀態爲已讀
// this.messageDAO.updateMessageState(new ObjectId(jsonNode.get("id").asText()), 2);
}else{
// 不需要做處理
}
} catch (Exception e) {
e.printStackTrace();
}
}
地址:D:\wsy\下載\黑馬—租房\新建文件夾(2)\day10-RocketMQ集羣、分佈式WebSocket實現以及地圖找房功能實現\代碼\itcast-haoke-im
其他項目保存到redis的數據,Ms推送到前端
地址:D:\wsy\下載\springboot整合\notice-master\notice
MS項目,應用了定時器
但是報錯404
項目是在springboot整合Mq的基礎上整合了ms
代碼:D:\wsy\project\排隊機\4.實現\code\v1.0\springboot-rocketmq-producer_rocketmq
參考網址:https://blog.csdn.net/qq_35387940/article/details/93483678