Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How not to use SockJS #14

Closed
Glamdring opened this issue Nov 21, 2013 · 18 comments
Closed

How not to use SockJS #14

Glamdring opened this issue Nov 21, 2013 · 18 comments

Comments

@Glamdring
Copy link

I don't need to use SockJS - I would just like to use a regular websocket. Stomp provides: Stomp.client("ws://:/endpoint"). However, that doesn't seem to work with this example. Can it work, and how to make it work like that.
Probably it would be nice ot have a page without SockJS.

@mikesir87
Copy link

Just wondering... why would you even want to do this? SockJS is only an abstraction layer that lets you create a persistent connection to the server without having to care about the actual transport protocol being used.

If you don't use SockJS, you'll then need to write your own logic (both server and client) to determine if websockets is supported and if not, what fallbacks are available. Having had to do that before, it's no fun. I'd be interested to find out why you would want to forgo using SockJS.

@Glamdring
Copy link
Author

Right now - because I want to experiment with all options - no sockjs, no stompjs, spring, javax.websocket, etc.

For a regular application you are right. My only concern is that it is yet-another-dependency, but that's rarely an issue, I agree.

@rstoyanchev
Copy link
Owner

Have you tried turning off SockJS support in the configuration? i.e. removing .withSockJS().

BTW you can also connect to a raw WebSocket (i.e. no SockJS frames) even with SockJS (i.e "/portfolio/websocket"). See the section on "Connecting to SockJS without the client" in the client page.

Also for what it's worth on the server-side at least using SockJS doesn't require any new dependencies.

@rstoyanchev
Copy link
Owner

To summarize, to remove the SockJS layer, remove .withSockJS() in WebSocketConfig.java and use new WebSocket('ws://localhost:8080/spring-websocket-portfolio/portfolio') on the client side.

@rstoyanchev
Copy link
Owner

It sounds very unusual. Is it possible to create a simple example some place that demonstrates the issue?

@francoisledroff
Copy link

All I needed was remove .withSockJs in the websocket config on the server side.
Then on the client, remove the import of sokjs js library
and use stomp nevertheless, replace

    var socket = new SockJS('/spring-websocket-portfolio/portfolio');
    var stompClient = Stomp.over(socket);

by

     var url = "ws://localhost:8080//spring-websocket-portfolio/portfolio?access_token="+authToken;
     var stompClient = Stomp.client(url);

why, would I do that?

wellI have 2 reasons:

@cooniur
Copy link

cooniur commented Jan 5, 2016

In Spring 4.2, just use the StandardWebSocketClient, and handle the message by implementing WebSocketHandler interface.

Something like:

def client = new StandardWebSocketClient()
client.doHandshake(new MyWebSocketHandler(), rtmWsUrl)

private static class MyWebSocketHandler implements WebSocketHandler {
    ....
}

@rstoyanchev
Copy link
Owner

Indeed, and the chapter in the reference documentation is organized starting with a sub-section on WebSocket followed by SockJS and then STOMP.

@yovinn
Copy link

yovinn commented Jan 20, 2017

7455

@yovinn
Copy link

yovinn commented Jan 20, 2017

i can not understand your mean!

@thoslin
Copy link

thoslin commented Apr 1, 2017

I had the same issue with OP. The scenario is that I tried to connect to spring websocket from iOS, however I didn't find any usable Stomp library that supports sockjs. So I tried to use Stomp without sockjs. Per the documentation https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-intro.

Or if connecting via WebSocket (without SockJS):

var socket = new WebSocket("/spring-websocket-portfolio/portfolio");
var stompClient = Stomp.over(socket);

stompClient.connect({}, function(frame) {
}

I removed the with-sockjs line from my configuration and tested the above code in chrome dev tools. However it didn't work. The error is URL invalid. Then I used the URL "ws://domain/spring-websocket-portfolio/portfolio", it then gave me 400 Bad Request.

So without sockjs, which url should I connect to?

@acrive82
Copy link

acrive82 commented Apr 5, 2017

Removing .withSockJS()

Caused by: java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found

@rstoyanchev
Copy link
Owner

So without sockjs, which url should I connect to?

The same.

No suitable default RequestUpgradeStrategy found

Old server version perhaps?

@acrive82
Copy link

acrive82 commented Apr 6, 2017

I use SpringBoot 1.5.2 with tomcat starter. I'm try to figure out why this happens : link

@rstoyanchev
Copy link
Owner

That links shows a different error than the "RequestUpgradStrategy not found".

@thoslin
Copy link

thoslin commented Apr 10, 2017

@rstoyanchev it's not working for me. I tried:

var ws = new WebSocket("ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio")

It gave the error:

WebSocket connection to 'ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio' failed: Error during WebSocket handshake: Unexpected response code: 400

while if I tried

var ws = new WebSocket("ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio/websocket")

It then gave the error

WebSocket connection to 'ws://127.0.0.1:9200/spring-websocket-portfolio/portfolio/websocket' failed: Error during WebSocket handshake: Unexpected response code: 404

@rstoyanchev
Copy link
Owner

If SockJS is turned off there is no point in trying /{endpoint}/websocket since that's a URL structure only supported with SockJS.

Well 400 means bad input. Look further. Response headers, DEBUG logging for org.springframework.web, step through with debugger if needed. You're missing something basic.

@nadirvishun
Copy link

this answer: Spring boot Websocket without SockJS
registry two same name endpoint:

        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry)
        {
                // This will allow you to use ws://localhost:8080/test to establish websocket connection
                registry.addEndpoint("/test");
                // This will allow you to use http://localhost:8080/test to establish websocket connection
                registry.addEndpoint("/test").withSockJS(); 
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants