webSocket应用

webSocket应用

1.依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>

</dependency>

2.webSocketConfig

@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}

}

3.SocketManager

@Service
public class WebSocketManager {


@Resource
private RedisTemplate<String, List<Session>> redisTemplate ;

public static Long onLineCount = 0L;

private static final String SESSION_KEY = "SESSIONKEY";

public void addSession(Long userId, String appCode, Session session) {

HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();

String key = StringUtils.join(appCode, ",", userId.toString());
boolean hasKey = clients.hasKey(SESSION_KEY, key);
if (hasKey) {
List<Session> sessions = clients.get(SESSION_KEY, key);
if (CollectionUtils.isEmpty(sessions)) {
List<Session> sessionList = new ArrayList<>();
sessionList.add(session);
clients.put(SESSION_KEY, key, sessionList);
} else {
sessions.add(session);
clients.put(SESSION_KEY, key, sessions);
}
} else {
List<Session> sessionList = new ArrayList<>();
sessionList.add(session);
clients.put(SESSION_KEY, key, sessionList);
}
onLineCount++;
}

public void deleteSession(Long userId, String appCode, Session session) {
String key = StringUtils.join(appCode, ",", userId.toString());
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
if (null == clients) {
return;
}
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
List<Session> sessions = clients.get(SESSION_KEY, key);
if (CollectionUtils.isEmpty(sessions)) {
clients.delete(SESSION_KEY, key);
} else {
if (sessions.contains(session)) {
if (1 == sessions.size()) {
clients.delete(SESSION_KEY, key);
}
sessions.remove(session);
clients.put(SESSION_KEY, key, sessions);
}
}
}
onLineCount--;
}

public  Map<String, List<Session>> getClients() {

HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
Map<String, List<Session>> lists = clients.entries(SESSION_KEY);
return lists;
}

public List<Session> getSession(Long userId, String appCode) {

String key = StringUtils.join(appCode, ",", userId.toString());
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();

return clients.get(SESSION_KEY, key);
}


public  List<Session> getAllSession() {
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
List<Session> allSession = new ArrayList<>();
List<List<Session>> lists = clients.values(SESSION_KEY);

for (List<Session> list : lists) {
allSession.addAll(list);
}
return allSession;
}
}

4.SocketWeb

@ServerEndpoint(value = "/message/{code1}/{code2}")
@Component
public class SocketWeb{

@OnOpen
public void onOpen(@PathParam("code1") String code1,
@PathParam("code2") String code2, Session session) {
    //socket 连接触发
}

@OnMessage
public void OnMessage(@PathParam("code1") String code1,
@PathParam("code2") String code2, Session session, String message) {
    //socket和客户端交互触发
}


@OnClose
public void OnClose(@PathParam("code1") String code1,
@PathParam("code2") String code2, Session session) {

//socket关闭触发
}

@OnError
public void OnError(Throwable throwable, Session session) {
    //异常触发
LOG.error(throwable.getMessage(), throwable);

}

}

4.webSocket 测试工具

http://www.blue-zero.com/WebSocket/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值