-
Notifications
You must be signed in to change notification settings - Fork 34
/
EventWebSocket.java
56 lines (46 loc) · 1.33 KB
/
EventWebSocket.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.enonic.xp.admin.event.impl;
import java.io.IOException;
import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
public final class EventWebSocket
extends Endpoint
{
private Session session;
private final WebSocketManager webSocketManager;
public EventWebSocket( final WebSocketManager webSocketManager )
{
this.webSocketManager = webSocketManager;
}
@Override
public void onOpen( final Session session, final EndpointConfig config )
{
this.session = session;
this.webSocketManager.registerSocket( this );
}
@Override
public void onClose( final Session session, final CloseReason closeReason )
{
this.webSocketManager.unregisterSocket( this );
this.session = null;
}
@Override
public void onError( final Session session, final Throwable error )
{
this.webSocketManager.unregisterSocket( this );
this.session = null;
}
public boolean isOpen()
{
return ( this.session != null ) && this.session.isOpen();
}
public void sendMessage( final String message )
throws IOException
{
if ( isOpen() )
{
this.session.getBasicRemote().sendText( message );
}
}
}