VASmalltalk – 0MQ 4.0, Stream Socket as a client

In the last posting I showed the usage of the new stream socket as a server socket under VASmalltalk by building a small HTTP server example. This time I want to show the usage of this socket as a client – making HTTP requests.

| aContext aZMQSocketConfiguration aSocket requestStream request identity|
"build the 0MQ context"
aContext := ZMQExtendedContext newContextNamed: 'StreamExample - Client'.
aContext startWithOneThread.

"defining the socket options"
aZMQSocketConfiguration := ZMQStreamSocket defaultSocketConfigurationAt: 
                               (ZMQEndpoint protocol: 'tcp' interface: '' port: 80).
  shouldConnectViaBind: false.
"creates the socket and connect it"
aSocket := ZMQStreamSocket newSocketWithConfiguration: aZMQSocketConfiguration in: aContext.

"the identity is needed for sending the request"
identity := aSocket getIdentityOption.

"building the request"
requestStream := WriteStream on: ByteArray new.
  nextPutAll: 'GET / HTTP/1.1' ;
  nextPutAll: Character lf asString;
  nextPutAll: 'host:' ;
  nextPutAll: Character lf asString ;
  nextPutAll: Character lf asString.

"sending the request"
request := ZMQTelegram  newOnSocket: aSocket.
	addFrame: (ZMQBinaryFrame newWithData: identity); 
	addFrame: (ZMQBinaryFrame newWithData: requestStream contents);

"the announcement block is simple - just print the response"			
aSocket announcer
  subscribe: ZMQTelegramReceivedAnnouncement 
  do: [ :announcement :announce |
    |   content telegram |
    telegram := announcement zmqTelegram.
    content := telegram frames last.
       cr ;
       show: content data asString.

The result is shown in trhe Transcript window ..

HTTP/1.1 200 OK
Date: Fri, 11 Oct 2013 05:57:35 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Transfer-Encoding: chunked

The most interesting stuff here is, that the requests sends more than one announcements. The receiving contents is delivered via 26 telegrams – each with a size 1430 and 2860 bytes.

This entry was posted in Smalltalk and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s