springboot与服务器推送技术整合,java程序员面试题解大全

return “ok”;

}

}

复制代码

对连接超时异常进行全局处理

@ExceptionHandler(AsyncRequestTimeoutException.class)

@ResponseBody

public String handleAsyncRequestTimeoutException(AsyncRequestTimeoutException e) {

return SseEmitter.event().data(“timeout!!”).build().stream()

.map(d -> d.getData().toString())

.collect(Collectors.joining());

}

复制代码

2.2访问测试

=======

http://localhost:8888/sse/test

http://localhost:8888/sse/payback

三、双向实时通信websocket

=================

3.1 整合websocket


org.springframework.boot

spring-boot-starter-websocket

复制代码

开启websocket功能

@Configuration

public class WebSocketConfig {

@Bean

public ServerEndpointExporter serverEndpointExporter() {

return new ServerEndpointExporter();

}

}

复制代码

3.2 websocket 用法实验


WebSocketServer内容的一些核心代码,websocket服务端代码

  1. @ServerEndpoint(value = “/ws/asset”)表示websocket的接口服务地址

  2. @OnOpen注解的方法,为连接建立成功时调用的方法

  3. @OnClose注解的方法,为连接关闭调用的方法

  4. @OnMessage注解的方法,为收到客户端消息后调用的方法

  5. @OnError注解的方法,为出现异常时调用的方法

/**

  • WebSocket服务端示例

*/

@Component

@Slf4j

@ServerEndpoint(value = “/ws/asset”)

public class WebSocketServer {

private static final AtomicInteger OnlineCount = new AtomicInteger(0);

// concurrent包的线程安全Set,用来存放每个客户端对应的Session对象。

private static CopyOnWriteArraySet SessionSet = new CopyOnWriteArraySet<>();

/**

  • 连接建立成功调用的方法

*/

@OnOpen

public void onOpen(Session session) throws IOException {

SessionSet.add(session);

int cnt = OnlineCount.incrementAndGet(); // 在线数加1

log.info(“有连接加入,当前连接数为:{}”, cnt);

SendMessage(session, “连接成功”);

}

/**

  • 连接关闭调用的方法

*/

@OnClose

public void onClose(Session session) {

SessionSet.remove(session);

int cnt = OnlineCount.decrementAndGet();

log.info(“有连接关闭,当前连接数为:{}”, cnt);

}

/**

  • 收到客户端消息后调用的方法

  • @param message 客户端发送过来的消息

*/

@OnMessage

public void onMessage(String message, Session session) throws IOException {

log.info(“来自客户端的消息:{}”,message);

SendMessage(session, “收到消息,消息内容:”+message);

}

/**

  • 出现错误

*/

public void onError(Session session, Throwable error) {

log.error(“发生错误:{},Session ID: {}”,error.getMessage(),session.getId());

}

/**

  • 发送消息,实践表明,每次浏览器刷新,session会发生变化。

  • @param session session

  • @param message 消息

*/

private static void SendMessage(Session session, String message) throws IOException {

session.getBasicRemote().sendText(String.format(“%s (From Server,Session ID=%s)”,message,session.getId()));

}

/**

  • 群发消息

  • @param message 消息

*/

public static void BroadCastInfo(String message) throws IOException {

for (Session session : SessionSet) {

if(session.isOpen()){

SendMessage(session, message);

}

}

}

/**

  • 指定Session发送消息

  • @param sessionId sessionId

  • @param message 消息

*/

public static void SendMessage(String sessionId,String message) throws IOException {

Session session = null;

for (Session s : SessionSet) {

if(s.getId().equals(sessionId)){

session = s;

break;

}

}

if(session!=null){

SendMessage(session, message);

} else{

log.warn(“没有找到你指定ID的会话:{}”,sessionId);

}

}

}

复制代码

客户端代码,做几次实验,自然明了代码的意思,先不要看代码,先看效果。public/wstest/html

websocket测试

WebSocket测试,在控制台查看测试信息输出!


http://localhost:8888/api/ws/sendOne?message=单发消息内容&id=none


http://localhost:8888/api/ws/sendAll?message=群发消息内容

请输入要发送给服务器端的消息:


发送服务器消息

关闭连接


信息:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值