Hi Norbert,
(I am CCing the mailing list).
On 12 Dec 2012, at 11:51, Norbert Hartl <
[hidden email]> wrote:
> Hi Sven,
>
> I'm playing with your WebSockets stuff at the moment. I'm asking myself how I can identify a client using Websockets. There is one delegate in the ZnServer that gets called from every connection. So I guess I need to use the WebSocket object handed over to the delegate. What would be the best information to use for identification? As there is only SocketStream involved I guess it isn't easy to get the IP address of the stream.
>
> Any hints appreciated,
>
> Norbert
Some answers/hints in random order:
ZnServer indeed has only one delegate but a delegate is just an object implementing #handleRequest: and #value: as a synonym, you can nest them easily. ZnDefaultServerDelegate (and the alternative ZnDispatcherDelegate) can do some primitive routing. Have a look at ZnWebSocketDelegate class>>#installExamplesInServer: to see what I mean.
WebSockets constitute a permanent single session connection by design. The easiest way to have some control over who opens a new connection is by relying on the standard HTTP authentication logic, IMO. ZnServer provides an easy to use (server wide) implementation through its authenticator. For example:
server authenticator: (ZnBasicAuthenticator username: 'foo' password: 'secret').
Obviously, in real life the authenticator will be a bit more elaborate. But you don't have to use this mechanism, look at its simple implementation and #authenticateRequest:do:, you could easily roll your own in a custom #handleRequest: The ZnServer authenticator makes it pluggable/independent.
Regarding the IP address, Zn adds a custom 'X-Zinc-Remote-Address' header to any request it reads. Given a socket stream, you get the IP address of the other side like this:
socketStream socket remoteAddress
HTH,
Sven
--
Sven Van Caekenberghe
http://stfx.euSmalltalk is the Red Pill