Flask接口签名sign原理与实例代码浅析
260
2023-06-14
Java中websocket消息推送的实现代码
一.服务层
package com.demo.websocket;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrehttp://nt.ConcurrentLinkedQueue;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Configuration
@EnableWebSocket
public class websocketListener implements WebSocketConfigurer, ServletContextListener{
private Concurrehttp://ntLinkedQueue
private WebSocketHandlerTest handler;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
handler = new WebSocketHandlerTest();
registry.addHandler(handler, "/ws/notifymessage.ws");
registry.addHandler(handler, "/ws/sockjs/notifymessage.ws").withSockJS();
new Thread(handler).start();
}
class WebSocketHandlerTest extends TextWebSocketHandler implements Runnable{
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
sessions.remove(session);
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
sessions.add(session);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super.handleTextMessage(session, message);
}
@Override
public void run() {
System.out.println("等待推送....");
try {
int i = 0;
for (;;) {
synchronized (this) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i%10==0){
nofity("消息推送测试......");
System.out.println("推送消息了....");
}else{
System.out.println("本次不推送消息了....");
}
i++;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("失败....");
}
}
private void nofity(String message) throws IOException {
Iterator
while (iterator.hasNext()) {
WebSocketSession session = iterator.next();
synchronized(session){
if(session.isOpen()){
session.sendMessage(new TextMessage(message));
}else{
iterator.remove();
}
}
}
}
}
}
二.前台界面监听
websocket测试界面
var websocketPath = "localhost:8080/demo-web";
if ('WebSocket' in window) {
websocket = new WebSocket("ws://"+websocketPath+"/ws/notifymessage.ws");
} else if ('MozWebSocket' in window) {
websocket = new MozWebSocket("ws://"+websocketPath+"/ws/notifymessage.ws");
} else {
websocket = new SockJS("ws://"+websocketPath+"/ws/notifymessage.ws");
}
websocket.onopen = function (evnt) {
};
websocket.onmessage = function (evnt) {
console.log(evnt);
};
websocket.onerror = function (evnt) {
};
websocket.onclose = function (evnt) {
}
注意web.xml中配置DispatcherServlet控制器
spring-servlet.xml空文件
以上所述是给大家介绍的Java中websocket消息推送的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~