package com.gxuwz.websocket.handler;
import java.io.IOException;
import java.util.ArrayList;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
public class SystemWebSocketHandler implements WebSocketHandler {
private static final ArrayList<WebSocketSession> users = new ArrayList<WebSocketSession>();;
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
users.add(session);
System.err.println("新增的请求IP:"+session.getRemoteAddress());
System.err.println("当前人数"+users.size());
}
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
sendMessageToUsers(message);
}
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
if(session.isOpen()){
session.close();
}
users.remove(session);
}
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
System.err.println(session.getId()+"离开了哦");
users.remove(session);
}
public boolean supportsPartialMessages() {
return false;
}
public void sendMessageToUsers(WebSocketMessage<?> message) {
for (WebSocketSession user : users) {
try {
if (user.isOpen()) {
user.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springWebsocket的简单例子
共31个文件
jar:11个
xml:4个
class:2个
需积分: 15 23 下载量 142 浏览量
2015-06-22
21:35:22
上传
评论
收藏 4.27MB RAR 举报
温馨提示
一个简单的websocket例子,适合新手参考,使用spring框架的websocket。很简单的例子
资源推荐
资源详情
资源评论
收起资源包目录
simple.rar (31个子文件)
simple
.project 2KB
.mymetadata 291B
src
applicationContext.xml 1KB
com
gxuwz
websocket
config
WebSocketConfig.java 1KB
handler
SystemWebSocketHandler.java 2KB
WebRoot
WEB-INF
classes
applicationContext.xml 1KB
com
gxuwz
websocket
config
WebSocketConfig.class 2KB
handler
SystemWebSocketHandler.class 3KB
lib
spring-context-4.0.2.RELEASE.jar 951KB
spring-websocket-4.0.2.RELEASE.jar 254KB
org.springframework.expression-3.1.1.RELEASE.jar 172KB
spring-core-4.0.2.RELEASE.jar 938KB
spring-webmvc-4.0.2.RELEASE.jar 645KB
spring-context-support-4.0.2.RELEASE.jar 132KB
commons-logging-1.1.jar 52KB
spring-webmvc-portlet-4.0.2.RELEASE.jar 171KB
org.springframework.aop-3.1.1.RELEASE.jar 324KB
spring-web-4.0.2.RELEASE.jar 649KB
spring-beans-4.0.2.RELEASE.jar 653KB
web.xml 850B
index.jsp 5KB
js
jquery.WebSocket.js 3KB
jquery.js 89KB
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 456B
.jsdtscope 500B
.classpath 529B
共 31 条
- 1
资源评论
fulunyong
- 粉丝: 6
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功