Teapot and Websockets

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Teapot and Websockets

Georges
Hello,

Is it somehow possible to have a Teapot server running within there a
WebsocketHandler (for instance ZnWebSocketChatroomHandler).

Something like:
Teapot on
 WS: '/ws-chatroom' -> [: aWebSocket | self registerWebSocket: aWebSocket ];
start.

Or is this totally wrong?

Georges



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Websockets

Attila Magyar
Hi Georges,

This is not supported at this moment. If I understand your example correctly
then

WS: '/ws-chatroom'  would match on an incoming WebSocket handshake request
and it would create and return new websocket. The same websocket would be
passed to the handler block for doing additional setup.

But seeing a more complete example would be helpful.



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Websockets

Georges
Hello Attila,

What I want is to have a Teapot Server running that somehow also can handle
websockets :)

In the Pharo Enterprise Book there is a chapter about WebSockets.
In there are examples how to setup a ZnServer with a ZnWebSocketDelegate,
for instance:

ZnServer stopDefault.
ZnServer startDefaultOn: 1701.
ZnServer default logToTranscript.
ZnServer default delegate
   map: 'ws-echo-client'
   to: [ :request | ZnResponse ok: (ZnEntity html: ZnWebSocketEchoHandler
clientHtml) ];
   map: 'ws-echo'
   to: (ZnWebSocketDelegate map: 'ws-echo' to: ZnWebSocketEchoHandler new).

I think it would be nice if this translates to:

Teapot on
        GET: '/ws-echo-client' -> ZnWebSocketEchoHandler clientHtml;
        WS: '/ws-echo' -> [: webSocket | ZnWebSocketEchoHandler new value:
webSocket];
        start.

But I do not know if this is correct syntax. (Is WS a correct Method? , does
it fit next to GET / PUT / POST etc ?)

Or is it better to do something like:

teapot := Teapot on.
teapot GET: '/ws-echo-client' -> ZnWebSocketEchoHandler clientHtml.
teapot server delegate map:  'ws-echo'
   to: (ZnWebSocketDelegate map: 'ws-echo' to: ZnWebSocketEchoHandler new).
teapot start.


I hope this makes it a bit more clear :)



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Websockets

Attila Magyar
Teapot already supports conditional routes, maybe using them instead of
adding the WS: message would be better.

I'm thinking domething like:

Teapot on
    GET: '/chat' -> whatever; when: [:req | req isWebSocket];
    start.

or just

Teapot on
    GET: '/chat' -> whatever; when: #isWebSocket
    start.

I'll play with this idea sometime.




--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Websockets

Georges
I added two methods:

Teapot>>WS: patternActionAssoc
        self
                addRouteMethod: (TeaMethodMatcher exactly: #GET)
                pattern: patternActionAssoc key
                action: (ZnWebSocketDelegate map: patternActionAssoc key to:
patternActionAssoc value)

ZnWebSocketDelegate>>teaEvalActionOnRequest: aTeaRequest

        ^self handleRequest: aTeaRequest

When I do:

Teapot on
        GET: '/ws-echo-client' -> ZnWebSocketEchoHandler clientHtml;
        WS: '/ws-echo' -> ZnWebSocketEchoHandler new;
        start.

And go to localhost:1701/ws-echo-client in my webbrowser I get:

WebSocket Test
ws://localhost:1701/ws-echo
CONNECTED
SENT: WebSocket rocks
RESPONSE: WebSocket rocks
DISCONNECTED

To avoid the WS: message:

Teapot on
   GET: '/ws-echo-client' -> ZnWebSocketEchoHandler clientHtml;
   GET: '/ws-echo' -> (ZnWebSocketDelegate map: '/ws-echo' to:
ZnWebSocketEchoHandler new);
   start


For now I will use the WS: message, and hopefully in the future you come
with a nice solution :)

Georges



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Websockets

Hans
In reply to this post by Attila Magyar
Attila Magyar wrote

> Teapot already supports conditional routes, maybe using them instead of
> adding the WS: message would be better.
>
> I'm thinking domething like:
>
> Teapot on
>     GET: '/chat' -> whatever; when: [:req | req isWebSocket];
>     start.
>
> or just
>
> Teapot on
>     GET: '/chat' -> whatever; when: #isWebSocket
>     start.
>
> I'll play with this idea sometime.
>
>
>
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

I've played a little bit with  this:

GET: '/chat' -> [:req  | Teapot forward: aReq toWebSocket: '/chat' ]; when:
...

The class method of Teapot does the "ZnWebSocketDelegate map: ... to... "
thing. It could handle the instances of the delegate per url. Don't know it
this matches the Teapot philosophy, it was just another variant to play
with.

Cheers

Hans



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Websockets

Hans
In reply to this post by Georges
Hi,

now I'm playing with a new type of TeaRoute>>TeaWebSocketRoute. It conrains
the ZnWebSocketDelegate. And I also use the WS: message.

I want to try to setup my Teapot fork to Github with IceBerg. Just an
Experiment :)

Cheers

Hans



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html