套接字重脚订阅可以使用Javascript,但不能使用Java客户端

时间:2019-05-31 18:17:06

标签: java spring sockets stomp subscribe

我有一台Java Spring Server,它具有一个可以向其发送消息的套接字端点:

@MessageMapping("/tron/matches/{matchId}/players/{playerId}") 

,并且在将两个请求发送到该端点之后,每秒将向/ tron / matches / {matchId}发送一条消息:

this.simpMessagingTemplate.convertAndSend("/tron/matches/" + match.getId(),
                new ObjectMapper().writeValueAsString(
                        new TronMatch(match)
                ));

当我使用Javascript进行尝试时,这非常完美,我可以发送消息,服务器可以接收消息,在游戏开始出现两条消息之后,客户端每秒都会收到一次更新。这是javascript:

function connect() {
    var socket = new SockJS("http://localhost:80/tron/connect");
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe("/tron/matches/5dd9e561-7f77-4e18-aa68-0bfa81c9598f", function (greeting) {
            showGreeting(greeting.body);
        });
    });
}

function sendMessage() {
    stompClient.send("/tron/matches/5dd9e561-7f77-4e18-aa68-0bfa81c9598f/players/1", {}, JSON.stringify(
        {ready: true,
        direction: 'DOWN'}
        ));
}

当我使用Java客户端尝试相同操作时,无论我尝试如何操作都失败。消息正确发送,服务器接收到消息,因此套接字连接似乎正常工作,但我只是想不通如何订阅(我目前正在使用静态matchId进行测试,以确保javascript完全订阅了与我的Java客户端相同的路径。这是代码:

        WebSocketClient simpleWebSocketClient = new StandardWebSocketClient();
        List<Transport> transports = new ArrayList<>(1);
        transports.add(new WebSocketTransport(simpleWebSocketClient));
        SockJsClient sockJsClient = new SockJsClient(transports);
        WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
        stompClient.setMessageConverter(new MappingJackson2MessageConverter());


        String url = "ws://localhost:80/tron/connect";
        StompSessionHandler sessionHandler = new SessionHandler();
        StompSession session = stompClient.connect(url, sessionHandler).get();

            System.out.println("Send ready to " + "/tron/matches/" + ticket.getGameId() + "/players/" + ticket.getPlayerId());
            //ClientMessage msg = new ClientMessage(userId, line);
            session.send("/tron/matches/" + ticket.getGameId() + "/players/" + ticket.getPlayerId(),
                        new UpdatePlayer(true, PlayerDirection.DOWN));

            System.out.println("Subscribe to /tron/matches/" + ticket.getGameId());

            session.subscribe("/tron/matches/" + ticket.getGameId(), new StompFrameHandler() {
                @Override
                public Type getPayloadType(StompHeaders headers) {
                    return TronMatch.class;
                }

                @Override
                public void handleFrame(StompHeaders headers, Object payload) {
                    System.out.println("Message received: " + payload.toString());
            }
        });

我也尝试在SessionHandler中进行订阅,但这也行不通:

public class SessionHandler extends StompSessionHandlerAdapter {

    StompSession stompSession;

    @Override
    public void afterConnected(
            StompSession session, StompHeaders connectedHeaders) {
        System.out.println("Successfully connected");
        this.stompSession = session;
        System.out.println("Subscribe");
        session.subscribe("/tron/matches/5dd9e561-7f77-4e18-aa68-0bfa81c9598f", this);
        ///tron/matches/5dd9e561-7f77-4e18-aa68-0bfa81c9598f
        //sendMessage( "testmessage from client");
    }
    @Override
    public void handleFrame(StompHeaders headers, Object payload) {
        System.out.println("Received sth");
        Message msg = (Message) payload;
        System.out.println("Received : " + msg.toString()+ " from : ");
    }
    public void sendMessage(String message){
        System.out.println("Send");
        //stompSession.send("/tron/matches/1/players/1", message);
    }
}

如何用Java示例中的正确方式正确地预订Java中的Socket端点?

0 个答案:

没有答案