WebSocket + SSL

480 views
Skip to first unread message

George Moschovitis

unread,
Jan 10, 2013, 1:07:28 PM1/10/13
to mi...@dartlang.org
Is it possible to use Websockets (client and server side) encrypted with SSL, i.e. wss://?
Are there any examples available?

thanks,
-g.

James Wendel

unread,
Jan 10, 2013, 2:41:12 PM1/10/13
to mi...@dartlang.org
Server should be possible with SecureServerSocket.  It takes a bit of work to setup as you have to have a certificate to start the server.  Check out some of the examples here:


I'm not entirely sure what you have to do WebSocket side to get Secure Websockets going, as I don't see any examples in the codebase.

Søren Gjesse

unread,
Jan 11, 2013, 3:09:07 AM1/11/13
to General Dart Discussion
This is possible.

For the server you set up a HttpsServer and configure a WebSocketHandler just like in a HttpServer.

For the client you connect using the HttpClient using a https:// URL to get a HttpClientConnection, and then creates a WebSocketClientConnection from, that.

Regards,
Søren


--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
 
 

James Wendel

unread,
Jan 11, 2013, 10:13:10 AM1/11/13
to mi...@dartlang.org
For the client, that would require importing the dart:io library, which is only server-side, correct?  How about if I wanted to do it client-side (using WebSocket)?

I started with the chat-client example found here: https://github.com/dart-lang/io-2012-dart-code-lab
And tried to get this working over SSL and have been unsuccessful so far.  I was able to get the chat-server up and running using https (I connected to it with a normal browser using https and it worked just fine).  I then changed the client side to use "wss" instead of "ws", but I haven't been able to get it to connect.

William Hesse

unread,
Jan 11, 2013, 11:04:53 AM1/11/13
to General Dart Discussion
I am working on making a working test and example of wss: websockets
on the client and server side. I'll file a bug.
> --
> Consider asking HOWTO questions at Stack Overflow:
> http://stackoverflow.com/tags/dart
>
>



--
William Hesse

George Moschovitis

unread,
Jan 11, 2013, 4:11:51 PM1/11/13
to mi...@dartlang.org
Let me better explain my use case:

I want to create a DartVM standalone application that connects (as a client) to an existing WebSocket service. I don't see anything WebSocket-related in that test case.

-g.

James Wendel

unread,
Jan 11, 2013, 5:07:51 PM1/11/13
to mi...@dartlang.org
For the server code, look at the Chat Client example.  Here's the HTTPS version I have going for it:

  HttpsServer server = new HttpsServer();
 
WebSocketHandler wsHandler = new WebSocketHandler();
  wsHandler
.onOpen = new ChatHandler(basePath).onOpen;
 
SecureServerSocket sock = new SecureServerSocket('127.0.0.1', port, 128, 'CN=localhost');
  server
.defaultRequestHandler = new StaticFileHandler(basePath).onRequest;
  server
.addRequestHandler((req) => req.path == "/ws", wsHandler.onRequest);
  server
.onError = (error) => print(error);
  server
.listenOn(sock);

I don't have code for the client side yet, but it would use HttpClient.  Your URL would be something like "https://127.0.0.1:1337/ws".  Once you have your HttpClientConnection established, you can create a WebSocketClientConnection from it.

I'm not sure how much you know about websockets, but they do a standard HTTP handshake before the websocket is established.  Wikipedia has a nice little example on it: http://en.wikipedia.org/wiki/Websocket#WebSocket_protocol_handshake
Message has been deleted

Søren Gjesse

unread,
Jan 15, 2013, 2:39:40 AM1/15/13
to General Dart Discussion
Thanks for trying this out, filed http://code.google.com/p/dart/issues/detail?id=7904.


On Sat, Jan 12, 2013 at 7:16 PM, George Moschovitis <george.mo...@gmail.com> wrote:
I tried:

  var client = new HttpClient(),
      conn = new WebSocketClientConnection(client.getUrl(new Uri.fromString('https://...')));

      conn.onMessage = (msg) {
        print(msg);
      };

      conn.onOpen = () {
        conn.send('hello');
      };

and get the following error:

Unhandled exception:
UnimplementedError: SecureSocket.available not implemented yet
#0      _SecureSocket.available (dart:io:7343:5)
#1      _WebSocketConnectionBase._startProcessing.<anonymous closure> (dart:io:8086:40)
#2      _SecureSocket._secureDataHandler._secureDataHandler (dart:io:7478:29)
#3      _SocketBase._multiplex (dart:io-patch:410:26)
#4      _SocketBase._sendToEventHandler.<anonymous closure> (dart:io-patch:512:20)
#5      _ReceivePortImpl._handleMessage (dart:isolate-patch:40:92)

any ideas?

-g.

--

George Moschovitis

unread,
Jan 15, 2013, 3:22:33 AM1/15/13
to mi...@dartlang.org

On Tuesday, January 15, 2013 9:39:40 AM UTC+2, Søren Gjesse wrote:

Thank you.

-g.

Søren Gjesse

unread,
Jan 15, 2013, 4:02:30 AM1/15/13
to General Dart Discussion
The web socket implementation has been changed to not rely on Scoket.available in http://code.google.com/p/dart/source/detail?r=17047.

The bug is still open, but it no longer blocks secure web sockets.

Regards,
Søren


--

George Moschovitis

unread,
Jan 15, 2013, 5:10:04 AM1/15/13
to mi...@dartlang.org
Cool, I  will wait for the change to propagate to the 'editor' version and try again.

-g.

George Moschovitis

unread,
Jan 23, 2013, 2:59:42 PM1/23/13
to mi...@dartlang.org

  var client = new HttpClient(),
      conn = new WebSocketClientConnection(client.getUrl(new Uri.fromString('https://...')));

      conn.onMessage = (msg) {
        print(msg);
      };

      conn.onOpen = () {
        conn.send('hello');
      };

I tried this again in the latest version of the SDK, and I get the following error:

1006 HttpParserException: Connection closed before full response header was received

or

1006 HttpParserException: Invalid request method

Any ideas?

-g.


 

Søren Gjesse

unread,
Jan 24, 2013, 8:57:44 AM1/24/13
to General Dart Discussion
I have just tried the following code

import "dart:io";
import "dart:uri";

main() {
  var client = new HttpClient(),
  conn = new WebSocketClientConnection(
      client.getUrl(new Uri.fromString('https://echo.websocket.org/')));
  conn.onOpen = () {
    conn.send('hello');
  };
  conn.onMessage = (msg) {
    print(msg);
    conn.close();
  };
}

With the latest Editor/SDK version 0.3.1_r17463 on both Windows and Linux, and could not reproduce the problem. On what exact version of the SDK are you seeing this problem?

Regards,
Søern




 

--

George Moschovitis

unread,
Jan 24, 2013, 2:53:52 PM1/24/13
to mi...@dartlang.org

With the latest Editor/SDK version 0.3.1_r17463 on both Windows and Linux, and could not reproduce the problem. On what exact version of the SDK are you seeing this problem?

I used a slightly older version, but you are right, your example is working. I will examine my code.

-g.  
Reply all
Reply to author
Forward
0 new messages