Java中websocket消息推送的实现代码

网友投稿 254 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 sessions = new ConcurrentLinkedQueue();

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 iterator = sessions.iterator();

while (iterator.hasNext()) {

WebSocketSession session = iterator.next();

synchronized(session){

if(session.isOpen()){

session.sendMessage(new TextMessage(message));

}else{

iterator.remove();

}

}

}

}

}

}

二.前台界面监听

websocket测试界面

注意web.xml中配置DispatcherServlet控制器

spring-servlet.xml空文件

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/spring-servlet.xml

1

spring

*.ws

以上所述是给大家介绍的Java中websocket消息推送的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JAVA中读取文件(二进制,字符)内容的几种方法总结
下一篇:spring中定时任务taskScheduler的详细介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~