aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/test/java/com/juick/server/tests/WebsocketClientHandler.java
blob: ae92ccf0aa69f3625e94a940990ef0a8827c8874 (plain) (blame)
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
package com.juick.server.tests;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

class WebsocketClientHandler extends TextWebSocketHandler {
    private final CountDownLatch latch;
    private WebSocketSession session;
    private String lastMessage;
    public WebsocketClientHandler(CountDownLatch latch) {
        this.latch = latch;
    }
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws IOException {
        this.session = session;
        session.sendMessage(new TextMessage("PING"));
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) {
        this.lastMessage = message.getPayload();
        latch.countDown();
    }

    public WebSocketSession getSession() {
        return session;
    }

    public String getLastMessage() {
        return lastMessage;
    }
}