用于连接Spring WebSocket的Uri路径

时间:2016-09-02 22:10:05

标签: java spring websocket spring-websocket java-websocket

我有一个使用Spring MVC的WebSocket应用程序,我根据this教程定义了它。配置WebSocket的文件如下:

package com.myapp.spring.web.controller;
import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.web.socket.server.standard.SpringConfigurator;


@ServerEndpoint(value="/serverendpoint", configurator = SpringConfigurator.class)

public class serverendpoint {

    @OnOpen
    public void handleOpen () {
        System.out.println("JAVA: Client is now connected...");
    }

    @OnMessage
    public String handleMessage (Session session, String message) throws IOException {

        if (message.equals("ping")) {
//            return "pong"
                session.getBasicRemote().sendText("pong");
        }
        else if (message.equals("close")) {
            handleClose();
            return null;
        }
        System.out.println("JAVA: Received from client: "+ message);
        MyClass mc = new MyClass(message);
        String res = mc.action();
        session.getBasicRemote().sendText(res);
        return res;
    }

    @OnClose
    public void handleClose() {
        System.out.println("JAVA: Client is now disconnected...");
    }

    @OnError
    public void handleError (Throwable t) {
        t.printStackTrace();
    }
}

我的问题是,如果我有一个Javascript客户端试图连接到这个WebSocket,如果从上面的@serverendpoint注释看到WebSocket映射到“/ serverendpoint”,我应该使用什么?

var wsUri = "??????"
var webSocket = new WebSocket(wsUri);

wsUri应该是什么?

这是我的Spring MVC项目层次结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

与运行http端口

的服务器的地址相同