Questions tagged [java-websocket]

Use this tag for questions related to Java based server side implementation of WebSocket.

Java SE has no standard API for WebSocket, there's a 3rd party library: Java-WebSocket.

Writing your own WebSocket Server

The org.java_websocket.server.WebSocketServer abstract class implements the server-side of the WebSocket Protocol. A WebSocket server by itself doesn't do anything except establish socket connections though HTTP. After that it's up to your subclass to add purpose.

Writing your own WebSocket Client

The org.java_websocket.server.WebSocketClient abstract class can connect to valid WebSocket servers. The constructor expects a valid ws:// URI to connect to. Important events onOpen, onClose, onMessage and onIOError get fired throughout the life of the WebSocketClient, and must be implemented in your subclass.


Java EE has a standard API for server side implementation of WebSocket: JSR356.

524 questions
88
votes
1 answer

Difference between ws and wss?

What is the procedure to change ws into wss? Whether wss is make upgrade over normal HTTP or wss works only HTTPS? webSocket = new WebSocket("ws://localhost:port/Esv/ocp"); works fine, when I changed ws to wss webSocket = new…
mahe
  • 963
  • 2
  • 7
  • 9
40
votes
1 answer

What is a simple way to implement a websocket client in Android? Is the following example correct?

I was trying to use a class that implements a WebSocket client in Android. But I get the following error: 12-07 11:22:46.286 31579-31579/com.domain.wsocketchat W/System: ClassLoader referenced unknown path:…
Kreshnik
  • 2,661
  • 5
  • 31
  • 39
30
votes
4 answers

Accessing ServletContext and HttpSession in @OnMessage of a JSR-356 @ServerEndpoint

I need to get the ServletContext from inside a @ServerEndpoint in order to find Spring ApplicationContext and lookup for a Bean. For the moment my best approach is to bind that bean in the JNDI naming context and lookup it in the Endpoint. Any…
Sombriks
  • 3,370
  • 4
  • 34
  • 54
25
votes
3 answers

Stomp.js is out of date and no longer maintained - which Stomp client to use with Spring Websockets?

I found it surprising that the current documentation for the Spring WebSocket Support directs users to use stomp.js for their client-side JavaScript implementation. This project, on the ReadMe on it's GitHub page, says: This project is no longer…
rikoe
  • 1,639
  • 1
  • 21
  • 29
20
votes
4 answers

Can websocket messages get lost or not?

I'm currently developing a Java WebSocket Client Application and I have to make sure that every message from the server is received by the client. Is it possible that I lose some messages (once they are sent from the server) due to a connection…
Adrian Krebs
  • 4,179
  • 5
  • 29
  • 44
19
votes
1 answer

Connecting to Stomp over WebSockets implemented using Spring 4 from Java client

I received a Web application, implemented with Spring using STOMP over WebSockets Messaging, similar to what's described here (with RabbitMQ at the backend). It runs on Tomcat, and I can connect to the application using regular URLs (e.g.…
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
16
votes
1 answer

How to call @SendTo from Normal Request Call i.e @RequestMapping

I have implemented Web Socket using Spring MVC and it is working fine for me i.e work from one browser to another browser which is open for those socket using this code. @MessageMapping("/hello") @SendTo("/topic/greetings") public…
Charnjeet Singh
  • 3,056
  • 6
  • 35
  • 65
15
votes
3 answers

Draft refuses handshake when using java-websocket library to connect to the coinbase exchange websocket stream

I am attempting to use the Java-Websocket library by TooTallNate to create a websocket client that receives messages from the coinbase exchange websocket stream. I am porting a program I made in Python to Java because of parallelisation bottlenecks…
zjuhasz
  • 1,489
  • 12
  • 30
14
votes
1 answer

Close websocket connection with Java

How to close a websocket connection using Java WebSocket API? I have used Java websocket API for both server and client end points. The application is working fine. But I don't know how to close the websocket, before the main thread ends. This is my…
UnahD
  • 867
  • 2
  • 10
  • 25
14
votes
5 answers

How to add headers in Java Websocket client

I am connecting to a websocket server in Java using javax.websocket classes. import javax.websocket.DeploymentException; import javax.websocket.Session; import javax.websocket.WebSocketContainer; import java.io.IOException; import…
kavai77
  • 6,282
  • 7
  • 33
  • 48
13
votes
1 answer

Why does sockJS add a '/info' to a given websocket url path

I want to open a websocket port with a "webapp/socket.do" path. When I use SockJS and try to initiate the call by code var socket = new SockJS('/webapp/socket.do'); stompClient = Stomp.over(socket); stompClient.connect({}, ... SockJS…
Lihkinisak
  • 749
  • 2
  • 7
  • 14
12
votes
1 answer

How to keep websocket alive

I am establishing WebSocket connectivity where the server is built using javax.websocket and the client is in JavaScript. This code is working fine. But after some time session is closed. I want to keep this session alive. I have two questions: If…
Shubham Dwivedi
  • 151
  • 1
  • 1
  • 9
12
votes
3 answers

Heroku Websockets with Java ECONNREFUSED (Connection refused)

I am writing a small game server using Java. I use TooTallNate-Java-Websockets library to create my websocket server. Everything works when I run my server on localhost , I can connect to it from everywhere.However when I submit my app to Heroku ,…
Ivelius
  • 4,953
  • 7
  • 30
  • 54
11
votes
1 answer

Java server JavaScript client WebSockets

I'm trying to do a connection between a server in Java and a JavaScript client but I'm getting this error on client side: WebSocket connection to 'ws://127.0.0.1:4444/' failed: Connection closed before receiving a handshake response It maybe…
agfac
  • 183
  • 1
  • 1
  • 12
10
votes
0 answers

'java.security.cert.CertificateExpiredException: NotAfter' upon connecting secure web socket

I am trying to connect to a secured web socket to consume a API. Below is the source code. Hosting environment configuration is JRE 1.7 and Tomcat 7. import java.net.URI; import javax.websocket.ClientEndpoint; import…
Ravi Jain
  • 1,452
  • 2
  • 17
  • 41
1
2 3
34 35