首页>代码>基于springboot+websocket的简单在线聊天室案例>/online_chat_room/src/main/java/com/lianchua/chatoline/websocket/ChatRoomServerEndpoint.java
package com.lianchua.chatoline.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.lianchua.chatoline.utils.WebSocketUtils.LIVING_SESSIONS_CACHE;
import static com.lianchua.chatoline.utils.WebSocketUtils.sendMessage;
import static com.lianchua.chatoline.utils.WebSocketUtils.sendMessageAll;

/**
 * 聊天室
 *
 * @author Levin
 * @since 2018/6/26 0026
 */
@RestController
@ServerEndpoint("/chat-room/{username}")
public class ChatRoomServerEndpoint {

    private static final Logger log = LoggerFactory.getLogger(ChatRoomServerEndpoint.class);

    @OnOpen
    public void openSession(@PathParam("username") String username, Session session) {
        LIVING_SESSIONS_CACHE.put(username, session);
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd E a hh:mm:ss");
        String time = df.format(date);
        String message = time + " 欢迎用户[" + username + "] 来到聊天室!\n";
        log.info(message);
        sendMessageAll(message);
    }

    @OnMessage
    public void onMessage(@PathParam("username") String username, String message) {
        log.info(message);
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd E a hh:mm:ss");
        String time = df.format(date);
        sendMessageAll(time + "用户[" + username + "] : " + message + "\n");
        System.gc();
    }

    @OnClose
    public void onClose(@PathParam("username") String username, Session session) {
        //当前的Session 移除
        LIVING_SESSIONS_CACHE.remove(username);
        //并且通知其他人当前用户已经离开聊天室了
        sendMessageAll("用户[" + username + "] 已经离开聊天室了!\n");
        try {
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        try {
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        throwable.printStackTrace();
    }


    @GetMapping("/chat-room/{sender}/to/{receive}")
    public void onMessage(@PathVariable("sender") String sender, @PathVariable("receive") String receive, String message) {
        sendMessage(LIVING_SESSIONS_CACHE.get(receive), "[" + sender + "]" + "-> [" + receive + "] : " + message);
        System.out.println(message);
    }

}
最近下载更多
JiangYing009  LV8 4月23日
微信网友_5930136709730304  LV4 2022年11月30日
天气预报  LV11 2022年8月14日
Kevin_001  LV6 2022年7月3日
姜广坤  LV14 2022年6月1日
liuxie  LV12 2022年5月12日
caratttt  LV2 2022年4月15日
XXXinXXccv  LV2 2022年2月15日
lf624645770  LV3 2021年8月27日
breakCY  LV15 2021年8月18日
最近浏览更多
IrvinXi 4月18日
暂无贡献等级
Rinkia 3月13日
暂无贡献等级
阿凡达  LV9 2月29日
Boss绝  LV8 2月27日
erqiu2013  LV18 2023年12月27日
fff2003  LV6 2023年12月27日
暂无贡献等级
1234567p  LV1 2023年12月4日
lalalla159  LV3 2023年12月2日
litchi955  LV11 2023年11月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友