-
Notifications
You must be signed in to change notification settings - Fork 58
/
MySessionHandler.java
36 lines (29 loc) · 1.23 KB
/
MySessionHandler.java
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.nibado.example.websocket.client;
import com.nibado.example.websocket.service.Greeting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import java.lang.reflect.Type;
@Slf4j
public class MySessionHandler extends StompSessionHandlerAdapter {
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
session.subscribe("/topic/greetings", this);
session.send("/app/hello", "{\"name\":\"Client\"}".getBytes());
log.info("New session: {}", session.getSessionId());
}
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
exception.printStackTrace();
}
@Override
public Type getPayloadType(StompHeaders headers) {
return Greeting.class;
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
log.info("Received: {}", ((Greeting) payload).getContent());
}
}