sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.69k stars 1.66k forks source link

Failed to connect, "Not a websocket connection response" #179

Closed huhuhong closed 8 years ago

huhuhong commented 8 years ago

Sample Code:

            WebSocket ws = new WebSocket("ws://localhost:8080/spring-websocket-stomp-apollo/chat");
           // ws.Origin = "http://localhost:8080";
            using (ws)
            {
                ws.OnOpen += ws_OnOpen;
                ws.OnError += ws_OnError;
                ws.OnMessage += ws_OnMessage;
                //ws.OnMessage += (sender, e) =>
                 // Console.WriteLine("Laputa says: " + e.Data);

                ws.Connect();
                //ws.Send("SUBSCRIBE id:sub-0 destination:/topic/mytopic\0");
                Console.ReadKey(true);
            }

Sample Result: image

Question: My server did return HTTP1.1 but somehow it still fail on Handshake. Any clues?

I have a StompJS Browser Client with Javascript as below and it was able to connect successfully.

    connect: function(){
      var socket = new SockJS('/spring-websocket-stomp-apollo/chat');
      stompClient = Stomp.over(socket);
      stompClient.connect("guest", "guest", function() {
        alert("connected!");
huhuhong commented 8 years ago

Help ! Help !

tporadowski commented 8 years ago

@huhuhong your server does not reply (at least to this request) correctly

The response should be more like:

HTTP/1.1 101 Switching Protocols
Server: websocket-sharp/1.0
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: PBiGdYTaidm4dmlhGPixbCo5fWc=

Please have a look at IsWebSocketResponse to see what's required in a proper websocket response.

huhuhong commented 8 years ago

Thanks a lot and issue had resolved.

Solution:

            WebSocket ws = new WebSocket("ws://localhost:8080/spring-websocket-stomp-apollo/chat/websocket");
            //WebSocket ws = new WebSocket("ws://localhost:8080/spring-websocket-stomp-apollo/chat");

Description: Previously i'm focusing on cloning the communication flow of stomp javascript client.

  1. It return HTTP 200 on the 1st call image
  2. Return HTTP 101 on the 2nd call image