ZydWebSocket.java 3.2 KB
Newer Older
Y
yadong.zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * MIT License
 * Copyright (c) 2018 yadong.zhang
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.zyd.blog.core.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/18 11:48
 * @since 1.0
 */
@ServerEndpoint(value = "/websocket")
@Component
public class ZydWebSocket {

45
    private static final Logger LOG = LoggerFactory.getLogger(ZydWebSocket.class);
Y
yadong.zhang 已提交
46 47 48 49 50 51 52
    /**
     * 初始在线人数
     */
    private static AtomicInteger onlineCount = new AtomicInteger(0);
    /**
     * 线程安全的socket集合
     */
53
    private static CopyOnWriteArraySet<Session> webSocketSet = new CopyOnWriteArraySet<>();
Y
yadong.zhang 已提交
54 55 56 57 58 59 60 61 62 63 64 65
    /**
     * 与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    private Session session;

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session) {
        webSocketSet.add(session);
        onlineCount.incrementAndGet();
66 67
        LOG.info("有链接加入,当前在线人数为: {}", getOnlineCount());
        WebSocketUtil.broadcast(getOnlineCount(), webSocketSet);
Y
yadong.zhang 已提交
68 69 70 71 72 73 74 75
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        onlineCount.decrementAndGet();
76 77
        LOG.info("有链接关闭,当前在线人数为: {}", getOnlineCount());
        WebSocketUtil.broadcast(getOnlineCount(), webSocketSet);
Y
yadong.zhang 已提交
78 79 80 81 82 83 84 85 86 87
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message
     *         客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
88 89
        LOG.info("{}来自客户端的消息:{}", session.getId(), message);
        WebSocketUtil.sendMessage(message, session);
Y
yadong.zhang 已提交
90 91
    }

92 93
    private String getOnlineCount() {
        return Integer.toString(onlineCount.get());
Y
yadong.zhang 已提交
94 95
    }
}