开发者社区> 问答> 正文

javaee websocket 服务器连接不上

我使用的框架是springmvc,那么使用@ServerEndpoint(value="/websocket")注解访问方式是什么,或者可以使用别的访问方式能够进入到这个类里面吗。

展开
收起
a123456678 2016-03-17 14:44:08 3655 0
1 条回答
写回答
取消 提交回答
  • package com.ucg.base.system.message.config.controller;
     
    import java.io.IOException;
     
    import javax.websocket.OnClose;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.server.ServerEndpoint;
     
    @ServerEndpoint(value="/websocket")
    public class WebSocketTest{
     
        @OnMessage
        public void onMessage(String message, Session session) 
            throws IOException, InterruptedException {
             
            // Print the client message for testing purposes
            System.out.println("Received: " + message);
             
            // Send the first message to the client
            session.getBasicRemote().sendText("This is the first server message");
             
            // Send 3 messages to the client every 5 seconds
            int sentMessages = 0;
            while(sentMessages < 3){
                Thread.sleep(5000);
                session.getBasicRemote().
                    sendText("This is an intermediate server message. Count: "
                        + sentMessages);
                sentMessages++;
            }
             
            // Send a final message to the client
            session.getBasicRemote().sendText("This is the last server message");
        }
         
        @OnOpen
        public void onOpen () {
            System.out.println("Client connected");
        }
     
        @OnClose
        public void onClose () {
            System.out.println("Connection closed");
        }
    }
    2019-07-17 19:05:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS计算与存储分离架构实践 立即下载