Dewfy
Dewfy

Reputation: 23614

Spring 4 WebSocket configure port

Using Spring 4 I need configure WebSocket use other port than HTTP. In other words by default user access to HTTP and WebSocket as follow:

http://server:9090/
ws://server:9090/

But I need do the follow:

http://server:9090/
ws://server:9999/

In code I have only following:

@Configuration
@EnableWebSocket
public class WebSocketConfig
    implements WebSocketConfigurer { 

Also I have Handler:

Handler extends TextWebSocketHandler {

Is there such ability in Spring?

Upvotes: 2

Views: 17253

Answers (2)

Kevendra
Kevendra

Reputation: 109

Spring WebSocket different port for ws:// protocol

Due to limitation and in order to use websockets on App Engine Flexible Environment, app need to connect directly to application instance using the instance's public external IP. This IP can be obtained from the metadata server.

All MVC/Rest (http://) call should still serve from 8080 and in App Engine Flexible Environment ws:// server from ws://external_ip:65080

working code

https://github.com/kevendra/springmvc-websocket-sample

http://localhost:8080/
ws://localhost:8080/

to work with App Engine need below

http://localhost:8080/
ws://localhost:65080/  - in local
ws://external_ip:65080/ - App engine

Ref:

Extends org.eclipse.jetty.websocket.server.WebSocketHandler and start server context to 65080, but I'm looking for server managed by spring

Upvotes: 1

a better oliver
a better oliver

Reputation: 26828

AFAIK all current implementations of websockets depend on a handshake via HTTP. After the handshake the existing connection is upgraded. You don't get a new one and the port stays the same. Basically all websocket connections start as HTTP connections.

As a side note the ports, IP addresses etc. are subject of the server, not the application itself.

It might be possible to configure your server so that two ports can be used for an application, but they would both be used for HTTP and websocket alike. On the other hand this might be useful in your situation.

Upvotes: 3

Related Questions