Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
WebSocketOpenClose.java - Session Open and Close
How to provide call back methods for session open and close events with WebSocket API?
✍: FYIcenter.com
Adding session open and close event call back methods can be done
with @OnOpen and @OnCLose annotated methods as shown below:
// Copyright (c) 2016 FYIcenter.com
import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.websocket.*;
@ClientEndpoint
public class WebSocketOpenClose {
public static void main(String[] args) throws Exception {
WebSocketContainer c = ContainerProvider.getWebSocketContainer();
URI u = new URI("wss://echo.websocket.org");
Session s = c.connectToServer(WebSocketOpenClose.class, u);
System.out.println("WebSocket session info:");
System.out.println(" Protocol version: "+s.getProtocolVersion());
System.out.println(" Request URI: "+s.getRequestURI());
RemoteEndpoint.Basic r = s.getBasicRemote();
r.sendText("Hello there!");
Thread.sleep(1000*2);
s.close();
}
@OnMessage
public void OnMessage(String message) {
System.out.println("-- Message received: "+message);
}
@OnOpen
public void OnOpen(Session session) {
System.out.println("-- Session started: "+session.getId());
}
@OnClose
public void OnClose(Session session, CloseReason reason) {
System.out.println("-- Session closed: "+session.getId()+" - "+reason);
}
}
You can compile and run the above example in a command window with Java SE JDK 8 and the tyrus-standalone-client.jar from Java EE 7 SDK:
>\fyicenter\jdk-1.8.0\bin\javac -cp \fyicenter\glassfish4\mq\lib\tyrus-standalone-client.jar WebSocketOpenClose.java >\fyicenter\jdk-1.8.0\bin\java -cp .;\fyicenter\glassfish4\mq\lib\tyrus-standalone-client.jar WebSocketOpenClose -- Session started: fefe38df-30fe-40d3-a18a-2a1e4d5bbcfd WebSocket session info: Protocol version: 13 Request URI: wss://echo.websocket.org -- Message received: Hello there! -- Session closed: fefe38df-30fe-40d3-a18a-2a1e4d5bbcfd - CloseReason[1000]
⇐ WebSocketEchoClient.java - WebSocket Echo Client
2018-01-16, ∼2645🔥, 0💬
Popular Posts:
commons-lang-1.0.1.jar is the JAR file for Apache Commons Lang 1.0.1, which provides a host of helpe...
io.jar is a component in iText Java library to provide input/output functionalities. iText Java libr...
How to read XML document from socket connections with the socket\DelayedInput.java provided in the A...
HttpComponents Core Source Code Files are provided in the source package file, httpcomponents-core-5...
How to download and install iText7-Core-7.1.4.zip? iText7-Core-7.1.4.zip is the binary package of iT...