Amber <> Squeak communication using websockets, round 2

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

Amber <> Squeak communication using websockets, round 2

Jacob Wagner
  Hi, a while back I tried using websockets to work with squeak, pharo
and amber, with some limited success.  I can't remember exactly why I
gave up on the idea, but for some reason it didn't work out for me.  I
posted on the newsgroup about my efforts.

  Well, I noticed a forum post from world.st in which WebClient and
WebServer were updated for squeak to use more modern websocket
protocols.  I decided to try them again.  I now have a fully
functional multiplayer game demo coded in amber in client and squeak
for server.  Using this solution I don't think you need to host amber
in any special way - you don't need to host it in node and you don't
need to use the recent one click solution.  Just serve your client
files via apache or whatever, and have a running squeak image on the
same domain (if it's not on the same domain it won't work because of
cross site scripting security blocks on all major browsers).

  First off, let me say that if you try this - you're probably better
off using squeak instead of pharo.  The package WebClient is made for
squeak, and I had trouble getting the pharo version to work in the
past and recently as well.  If you want to try to get it to work in
pharo, you'll need to figure out how to install 'Installer' first, (I
tried installing it and it crashed my image).

  To get this whole thing to work, get squeak and install the latest
WebClient package (it supports websockets and comes with WebServer
class as well).  In amber no special libraries are necessary, at least
on chrome.  (It should work in firefox and other major browsers as
well).

  Once you've got your squeak image running, it's a simple matter of
using standard WebSocket object in amber/javascript and modifying the
WebSocket class >> example method in squeak.

  In case you have problems getting started, here is my cheat sheet
for useful methods on both sides:


  Amber:

<socket = new WebSocket("ws://www.zenchess.com:4003/broadcast");>.

^^ modify the above code to point to your web domain and the port that
WebSocket example is running on.

<socket.onopen = function(){
    alert("Socket has been opened!");
        self._onOpen();
        }  >.

^^ There's the function you register with your socket object that gets
called when the socket is opened for the first time.  self._onOpen();
is javascript code that will call the onOpen method in the class that
it is running from.  This lets me write an onOpen method in complete
smalltalk.  I couldn't figure out how to convert the above javascript
code into the smalltalk equivalent, so that is why I am using the < >
's .


<socket.onmessage = function(msg){

        data = msg.data;>.


Hopefully the above code and explanation can help anyone that is
looking for amber>regular smalltalk image communication.  I know there
are other solutions but websocket is actually a really good one, since
it's 2-way and does not have to continuously poll to get updates.  By
the way there are some JSON convenience methods for decoding and
encoding JSON in squeak in the WebUtils package of WebClient that came
in handy as well.

  I've been coding a multiplayer turn based strategy game in Ludus/
Amber/Squeak and it's coming along nicely, I'll be sure to demo it
here first. :P














<zenchess@gmail.com>
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Nicolas Petton
On 20/02/12 06:55, Zenchess wrote:


Did you know you can that in Smalltalk:

>    Amber:
>
> <socket = new WebSocket("ws://www.zenchess.com:4003/broadcast");>.

socket := WebSocket new value: 'ws://www.zenchess.com:4003/broadcast'.

>
> ^^ modify the above code to point to your web domain and the port that
> WebSocket example is running on.
>
> <socket.onopen = function(){
>     alert("Socket has been opened!");
> self._onOpen();
> }>.
>


socket onopen: [
        window alert: 'socket open'.
        self onOpen]

>
> <socket.onmessage = function(msg){
>
> data = msg.data;>.

socket onmessage: [:msg | data := mdg data]


Cheers,
Nico


--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Bernat Romagosa
Cool stuff! Welcome back!

Can we get a URL to your demo app to take a look at it?

Cheers,

2012/2/20 Nicolas Petton <[hidden email]>
On 20/02/12 06:55, Zenchess wrote:


Did you know you can that in Smalltalk:


  Amber:

<socket = new WebSocket("ws://www.zenchess.com:4003/broadcast");>.

socket := WebSocket new value: 'ws://www.zenchess.com:4003/broadcast'.



^^ modify the above code to point to your web domain and the port that
WebSocket example is running on.

<socket.onopen = function(){
        alert("Socket has been opened!");
       self._onOpen();
       }>.



socket onopen: [
       window alert: 'socket open'.
       self onOpen]



<socket.onmessage = function(msg){

       data = msg.data;>.

socket onmessage: [:msg | data := mdg data]


Cheers,
Nico


--
Nicolas Petton
http://nicolas-petton.fr



--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Jacob Wagner
In reply to this post by Nicolas Petton
  Hi Nicolas, I tried your version of the code, and got this:

"function WebSocket() { [native code] } does not understand #new"


On Feb 20, 3:50 am, Nicolas Petton <[hidden email]> wrote:

> On 20/02/12 06:55, Zenchess wrote:
>
> Did you know you can that in Smalltalk:
>
> >    Amber:
>
> > <socket = new WebSocket("ws://www.zenchess.com:4003/broadcast");>.
>
> socket := WebSocket new value: 'ws://www.zenchess.com:4003/broadcast'.
>
>
>
> > ^^ modify the above code to point to your web domain and the port that
> > WebSocket example is running on.
>
> > <socket.onopen = function(){
> >             alert("Socket has been opened!");
> >    self._onOpen();
> >    }>.
>
> socket onopen: [
>         window alert: 'socket open'.
>         self onOpen]
>
>
>
> > <socket.onmessage = function(msg){
>
> >    data = msg.data;>.
>
> socket onmessage: [:msg | data := mdg data]
>
> Cheers,
> Nico
>
> --
> Nicolas Pettonhttp://nicolas-petton.fr
<zenchess@gmail.com>
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Jacob Wagner
In reply to this post by Bernat Romagosa
  I'm almost finished making the game, so I'd like to finish that
first.


> Cool stuff! Welcome back!
>
> Can we get a URL to your demo app to take a look at it?
>
> Cheers,
>
<zenchess@gmail.com>
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Nicolas Petton
In reply to this post by Jacob Wagner
On 22/02/12 09:45, Zenchess wrote:
>    Hi Nicolas, I tried your version of the code, and got this:
>
> "function WebSocket() { [native code] } does not understand #new"


Yeah, there's an error in the code I wrote. It should be:
socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'

But anyway WebSocket isn't a normal function, so it doesn't seem to work
in this particular case :/

Cheers,
Nico

>
>
> On Feb 20, 3:50 am, Nicolas Petton<[hidden email]>  wrote:
>> On 20/02/12 06:55, Zenchess wrote:
>>
>> Did you know you can that in Smalltalk:
>>
>>>     Amber:
>>
>>> <socket = new WebSocket("ws://www.zenchess.com:4003/broadcast");>.
>>
>> socket := WebSocket new value: 'ws://www.zenchess.com:4003/broadcast'.
>>
>>
>>
>>> ^^ modify the above code to point to your web domain and the port that
>>> WebSocket example is running on.
>>
>>> <socket.onopen = function(){
>>>              alert("Socket has been opened!");
>>>     self._onOpen();
>>>     }>.
>>
>> socket onopen: [
>>          window alert: 'socket open'.
>>          self onOpen]
>>
>>
>>
>>> <socket.onmessage = function(msg){
>>
>>>     data = msg.data;>.
>>
>> socket onmessage: [:msg | data := mdg data]
>>
>> Cheers,
>> Nico
>>
>> --
>> Nicolas Pettonhttp://nicolas-petton.fr


--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Amber Milan Eskridge
In reply to this post by Jacob Wagner
Congratulations. Looking forward to play a game.

On Wed, Feb 22, 2012 at 9:46 AM, Zenchess <[hidden email]> wrote:
 I'm almost finished making the game, so I'd like to finish that
first.


> Cool stuff! Welcome back!
>
> Can we get a URL to your demo app to take a look at it?
>
> Cheers,
>

Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Ankh'nAton
In reply to this post by Nicolas Petton


On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:

> On 22/02/12 09:45, Zenchess wrote:
>
> >    Hi Nicolas, I tried your version of the code, and got this:
>
> > "function WebSocket() { [native code] } does not understand #new"
>
> Yeah, there's an error in the code I wrote. It should be:
> socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>
> But anyway WebSocket isn't a normal function, so it doesn't seem to work
> in this particular case :/
>

I get an error on Chrome 12.0 and FF 10.0 saying:

  function WebSocket() { [native code] } does not understand
#newValue:"

Regards...
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

jtuchel
Hi,

I am jumping on this thread as I saw some examples of how you could possible open a websocket from within Amber.
Unfortunately, none of these alternatives work for me:


socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
socket := WebSocket new value: 'ws://echo.websocket.org '. --> Debugger
socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a Debugger, no reaction at all, and firebug doesn't log any network traffic

So what is the correct way to instantiate and open a websocket in Amber?

Joachim

Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:


On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:

> On 22/02/12 09:45, Zenchess wrote:
>
> >    Hi Nicolas, I tried your version of the code, and got this:
>
> > "function WebSocket() { [native code] } does not understand #new"
>
> Yeah, there's an error in the code I wrote. It should be:
> socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>
> But anyway WebSocket isn't a normal function, so it doesn't seem to work
> in this particular case :/
>

I get an error on Chrome 12.0 and FF 10.0 saying:

  function WebSocket() { [native code] } does not understand
#newValue:"

Regards...
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Manfred Kröhnert

Maybe you encounter problems with the same-origin policy or your browser does not support websockets.

Best,
Manfred

Am 12.10.2012 15:00 schrieb "[hidden email]" <[hidden email]>:
Hi,

I am jumping on this thread as I saw some examples of how you could possible open a websocket from within Amber.
Unfortunately, none of these alternatives work for me:


socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
socket := WebSocket new value: 'ws://echo.websocket.org '. --> Debugger
socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a Debugger, no reaction at all, and firebug doesn't log any network traffic

So what is the correct way to instantiate and open a websocket in Amber?

Joachim

Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:


On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:

> On 22/02/12 09:45, Zenchess wrote:
>
> >    Hi Nicolas, I tried your version of the code, and got this:
>
> > "function WebSocket() { [native code] } does not understand #new"
>
> Yeah, there's an error in the code I wrote. It should be:
> socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>
> But anyway WebSocket isn't a normal function, so it doesn't seem to work
> in this particular case :/
>

I get an error on Chrome 12.0 and FF 10.0 saying:

  function WebSocket() { [native code] } does not understand
#newValue:"

Regards...
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Nicolas Petton
In reply to this post by jtuchel

Hi!

WebSocket is a special object so you cannot use BlockClosure >> #new:

Here's how I do it:

socket := <new WebSocket(aString)>.
socket at: 'onmessage' put: [ :message | self handleMessage: message ].
socket at: 'onerror' put: [ :err | console log: err ]

A running example in the workspace:

| socket |

<socket = new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | console log: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]

Cheers,
Nico

"[hidden email]" <[hidden email]> writes:

> Hi,
>
> I am jumping on this thread as I saw some examples of how you could
> possible open a websocket from within Amber.
> Unfortunately, none of these alternatives work for me:
>
>
> socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new value: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
>  socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
> socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a
> Debugger, no reaction at all, and firebug doesn't log any network traffic
>
> So what is the correct way to instantiate and open a websocket in Amber?
>
> Joachim
>
> Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:
>>
>>
>>
>> On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:
>> > On 22/02/12 09:45, Zenchess wrote:
>> >
>> > >    Hi Nicolas, I tried your version of the code, and got this:
>> >
>> > > "function WebSocket() { [native code] } does not understand #new"
>> >
>> > Yeah, there's an error in the code I wrote. It should be:
>> > socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>> >
>> > But anyway WebSocket isn't a normal function, so it doesn't seem to work
>> > in this particular case :/
>> >
>>
>> I get an error on Chrome 12.0 and FF 10.0 saying:
>>
>>   function WebSocket() { [native code] } does not understand
>> #newValue:"
>>
>> Regards...
>>

--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

jtuchel
Hi Nicolas,

thanks a lot.

Here's the working example (you had a type in it)

| socket |

socket := <new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | console log: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ].

Great!

Joachim

Am Freitag, 12. Oktober 2012 15:26:57 UTC+2 schrieb nicolas petton:

Hi!

WebSocket is a special object so you cannot use BlockClosure >> #new:

Here's how I do it:

socket := <new WebSocket(aString)>.
socket at: 'onmessage' put: [ :message | self handleMessage: message ].
socket at: 'onerror' put: [ :err | console log: err ]

A running example in the workspace:

| socket |

<socket = new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | console log: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]

Cheers,
Nico

"<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fLAcgVzB8dUJ">jtu...@..." <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fLAcgVzB8dUJ">jtu...@...> writes:

> Hi,
>
> I am jumping on this thread as I saw some examples of how you could
> possible open a websocket from within Amber.
> Unfortunately, none of these alternatives work for me:
>
>
> socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new value: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
>  socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
> socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a
> Debugger, no reaction at all, and firebug doesn't log any network traffic
>
> So what is the correct way to instantiate and open a websocket in Amber?
>
> Joachim
>
> Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:
>>
>>
>>
>> On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:
>> > On 22/02/12 09:45, Zenchess wrote:
>> >
>> > >    Hi Nicolas, I tried your version of the code, and got this:
>> >
>> > > "function WebSocket() { [native code] } does not understand #new"
>> >
>> > Yeah, there's an error in the code I wrote. It should be:
>> > socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>> >
>> > But anyway WebSocket isn't a normal function, so it doesn't seem to work
>> > in this particular case :/
>> >
>>
>> I get an error on Chrome 12.0 and FF 10.0 saying:
>>
>>   function WebSocket() { [native code] } does not understand
>> #newValue:"
>>
>> Regards...
>>

--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

jtuchel
Hi again,

I seem to not understand something about the use of a web socket.
GIven the following snippet in a workspace:

| socket |

socket := <new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | window alert: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]. 
socket send: 'This is a message to the Echo Service'.
socket close

I'd expect to see two alerts. 
But I only see the first one that gets sent to the echo service on open. using send: seems to not work. What's wrong here?

Joachim 

Am Freitag, 12. Oktober 2012 15:49:44 UTC+2 schrieb [hidden email]:
Hi Nicolas,

thanks a lot.

Here's the working example (you had a type in it)

| socket |

socket := <new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | console log: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ].

Great!

Joachim

Am Freitag, 12. Oktober 2012 15:26:57 UTC+2 schrieb nicolas petton:

Hi!

WebSocket is a special object so you cannot use BlockClosure >> #new:

Here's how I do it:

socket := <new WebSocket(aString)>.
socket at: 'onmessage' put: [ :message | self handleMessage: message ].
socket at: 'onerror' put: [ :err | console log: err ]

A running example in the workspace:

| socket |

<socket = new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | console log: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]

Cheers,
Nico

"[hidden email]" <[hidden email]> writes:

> Hi,
>
> I am jumping on this thread as I saw some examples of how you could
> possible open a websocket from within Amber.
> Unfortunately, none of these alternatives work for me:
>
>
> socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new value: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
>  socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
> socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a
> Debugger, no reaction at all, and firebug doesn't log any network traffic
>
> So what is the correct way to instantiate and open a websocket in Amber?
>
> Joachim
>
> Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:
>>
>>
>>
>> On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:
>> > On 22/02/12 09:45, Zenchess wrote:
>> >
>> > >    Hi Nicolas, I tried your version of the code, and got this:
>> >
>> > > "function WebSocket() { [native code] } does not understand #new"
>> >
>> > Yeah, there's an error in the code I wrote. It should be:
>> > socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>> >
>> > But anyway WebSocket isn't a normal function, so it doesn't seem to work
>> > in this particular case :/
>> >
>>
>> I get an error on Chrome 12.0 and FF 10.0 saying:
>>
>>   function WebSocket() { [native code] } does not understand
>> #newValue:"
>>
>> Regards...
>>

--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Andy Burnett
In case it is of any use, we have just added websocket support to Pharo via Zinc.

Cheers
Andy

On Fri, Oct 12, 2012 at 1:27 PM, [hidden email] <[hidden email]> wrote:
Hi again,

I seem to not understand something about the use of a web socket.
GIven the following snippet in a workspace:

| socket |

socket := <new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | window alert: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]. 
socket send: 'This is a message to the Echo Service'.
socket close

I'd expect to see two alerts. 
But I only see the first one that gets sent to the echo service on open. using send: seems to not work. What's wrong here?

Joachim 

Am Freitag, 12. Oktober 2012 15:49:44 UTC+2 schrieb [hidden email]:
Hi Nicolas,

thanks a lot.

Here's the working example (you had a type in it)

| socket |

socket := <new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | console log: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ].

Great!

Joachim

Am Freitag, 12. Oktober 2012 15:26:57 UTC+2 schrieb nicolas petton:

Hi!

WebSocket is a special object so you cannot use BlockClosure >> #new:

Here's how I do it:

socket := <new WebSocket(aString)>.
socket at: 'onmessage' put: [ :message | self handleMessage: message ].
socket at: 'onerror' put: [ :err | console log: err ]

A running example in the workspace:

| socket |

<socket = new WebSocket("ws://echo.websocket.org")>.
socket at: 'onmessage' put: [ :m | console log: m data ].
socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]

Cheers,
Nico

"[hidden email]" <[hidden email]> writes:

> Hi,
>
> I am jumping on this thread as I saw some examples of how you could
> possible open a websocket from within Amber.
> Unfortunately, none of these alternatives work for me:
>
>
> socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new value: 'ws://echo.websocket.org '. --> Debugger
> socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
>  socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
> socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a
> Debugger, no reaction at all, and firebug doesn't log any network traffic
>
> So what is the correct way to instantiate and open a websocket in Amber?
>
> Joachim
>
> Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:
>>
>>
>>
>> On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:
>> > On 22/02/12 09:45, Zenchess wrote:
>> >
>> > >    Hi Nicolas, I tried your version of the code, and got this:
>> >
>> > > "function WebSocket() { [native code] } does not understand #new"
>> >
>> > Yeah, there's an error in the code I wrote. It should be:
>> > socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>> >
>> > But anyway WebSocket isn't a normal function, so it doesn't seem to work
>> > in this particular case :/
>> >
>>
>> I get an error on Chrome 12.0 and FF 10.0 saying:
>>
>>   function WebSocket() { [native code] } does not understand
>> #newValue:"
>>
>> Regards...
>>

--
Nicolas Petton
http://nicolas-petton.fr

Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

Nicolas Petton
In reply to this post by jtuchel

It looks like you do a #send: before the connection is open.

Nico

"[hidden email]" <[hidden email]> writes:

> Hi again,
>
> I seem to not understand something about the use of a web socket.
> GIven the following snippet in a workspace:
>
> | socket |
>
> socket := <new WebSocket("ws://echo.websocket.org")>.
> socket at: 'onmessage' put: [ :m | window alert: m data ].
> socket at: 'onopen'  put: [ socket send: 'hello from Amber' ].
> socket send: 'This is a message to the Echo Service'.
> socket close
>
> I'd expect to see two alerts.
> But I only see the first one that gets sent to the echo service on open.
> using send: seems to not work. What's wrong here?
>
> Joachim
>
> Am Freitag, 12. Oktober 2012 15:49:44 UTC+2 schrieb [hidden email]:
>>
>> Hi Nicolas,
>>
>> thanks a lot.
>>
>> Here's the working example (you had a type in it)
>>
>> | socket |
>>
>> socket := <new WebSocket("ws://echo.websocket.org")>.
>> socket at: 'onmessage' put: [ :m | console log: m data ].
>> socket at: 'onopen'  put: [ socket send: 'hello from Amber' ].
>>
>> Great!
>>
>> Joachim
>>
>> Am Freitag, 12. Oktober 2012 15:26:57 UTC+2 schrieb nicolas petton:
>>>
>>>
>>> Hi!
>>>
>>> WebSocket is a special object so you cannot use BlockClosure >> #new:
>>>
>>> Here's how I do it:
>>>
>>> socket := <new WebSocket(aString)>.
>>> socket at: 'onmessage' put: [ :message | self handleMessage: message ].
>>> socket at: 'onerror' put: [ :err | console log: err ]
>>>
>>> A running example in the workspace:
>>>
>>> | socket |
>>>
>>> <socket = new WebSocket("ws://echo.websocket.org")>.
>>> socket at: 'onmessage' put: [ :m | console log: m data ].
>>> socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]
>>>
>>> Cheers,
>>> Nico
>>>
>>> "[hidden email]" <[hidden email]> writes:
>>>
>>> > Hi,
>>> >
>>> > I am jumping on this thread as I saw some examples of how you could
>>> > possible open a websocket from within Amber.
>>> > Unfortunately, none of these alternatives work for me:
>>> >
>>> >
>>> > socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
>>> > socket := WebSocket new value: 'ws://echo.websocket.org '. -->
>>> Debugger
>>> > socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
>>> >  socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
>>> > socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a
>>> > Debugger, no reaction at all, and firebug doesn't log any network
>>> traffic
>>> >
>>> > So what is the correct way to instantiate and open a websocket in
>>> Amber?
>>> >
>>> > Joachim
>>> >
>>> > Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:
>>> >>
>>> >>
>>> >>
>>> >> On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:
>>> >> > On 22/02/12 09:45, Zenchess wrote:
>>> >> >
>>> >> > >    Hi Nicolas, I tried your version of the code, and got this:
>>> >> >
>>> >> > > "function WebSocket() { [native code] } does not understand #new"
>>> >> >
>>> >> > Yeah, there's an error in the code I wrote. It should be:
>>> >> > socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>>>
>>> >> >
>>> >> > But anyway WebSocket isn't a normal function, so it doesn't seem to
>>> work
>>> >> > in this particular case :/
>>> >> >
>>> >>
>>> >> I get an error on Chrome 12.0 and FF 10.0 saying:
>>> >>
>>> >>   function WebSocket() { [native code] } does not understand
>>> >> #newValue:"
>>> >>
>>> >> Regards...
>>> >>
>>>
>>> --
>>> Nicolas Petton
>>> http://nicolas-petton.fr 
>>>
>>
--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

jtuchel

ouch! of course! thanks for your tip! got it working.

Am Samstag, 13. Oktober 2012 14:44:10 UTC+2 schrieb nicolas petton:

It looks like you do a #send: before the connection is open.

Nico

"<a href="javascript:" target="_blank" gdf-obfuscated-mailto="4vrZGRJXqnYJ">jtu...@..." <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="4vrZGRJXqnYJ">jtu...@...> writes:

> Hi again,
>
> I seem to not understand something about the use of a web socket.
> GIven the following snippet in a workspace:
>
> | socket |
>
> socket := <new WebSocket("ws://echo.websocket.org")>.
> socket at: 'onmessage' put: [ :m | window alert: m data ].
> socket at: 'onopen'  put: [ socket send: 'hello from Amber' ].
> socket send: 'This is a message to the Echo Service'.
> socket close
>
> I'd expect to see two alerts.
> But I only see the first one that gets sent to the echo service on open.
> using send: seems to not work. What's wrong here?
>
> Joachim
>
> Am Freitag, 12. Oktober 2012 15:49:44 UTC+2 schrieb [hidden email]:
>>
>> Hi Nicolas,
>>
>> thanks a lot.
>>
>> Here's the working example (you had a type in it)
>>
>> | socket |
>>
>> socket := <new WebSocket("ws://echo.websocket.org")>.
>> socket at: 'onmessage' put: [ :m | console log: m data ].
>> socket at: 'onopen'  put: [ socket send: 'hello from Amber' ].
>>
>> Great!
>>
>> Joachim
>>
>> Am Freitag, 12. Oktober 2012 15:26:57 UTC+2 schrieb nicolas petton:
>>>
>>>
>>> Hi!
>>>
>>> WebSocket is a special object so you cannot use BlockClosure >> #new:
>>>
>>> Here's how I do it:
>>>
>>> socket := <new WebSocket(aString)>.
>>> socket at: 'onmessage' put: [ :message | self handleMessage: message ].
>>> socket at: 'onerror' put: [ :err | console log: err ]
>>>
>>> A running example in the workspace:
>>>
>>> | socket |
>>>
>>> <socket = new WebSocket("ws://echo.websocket.org")>.
>>> socket at: 'onmessage' put: [ :m | console log: m data ].
>>> socket at: 'onopen'  put: [ socket send: 'hello from Amber' ]
>>>
>>> Cheers,
>>> Nico
>>>
>>> "[hidden email]" <[hidden email]> writes:
>>>
>>> > Hi,
>>> >
>>> > I am jumping on this thread as I saw some examples of how you could
>>> > possible open a websocket from within Amber.
>>> > Unfortunately, none of these alternatives work for me:
>>> >
>>> >
>>> > socket := WebSocket newValue: 'ws://echo.websocket.org '. --> Debugger
>>> > socket := WebSocket new value: 'ws://echo.websocket.org '. -->
>>> Debugger
>>> > socket := WebSocket new: 'ws://echo.websocket.org '. --> Debugger
>>> >  socket := WebSocket value: 'ws://echo.websocket.org '. --> Debugger
>>> > socket := <new WebSocket("ws://echo.websocket.org ");>. --> not even a
>>> > Debugger, no reaction at all, and firebug doesn't log any network
>>> traffic
>>> >
>>> > So what is the correct way to instantiate and open a websocket in
>>> Amber?
>>> >
>>> > Joachim
>>> >
>>> > Am Freitag, 24. Februar 2012 15:57:34 UTC+1 schrieb Tom:
>>> >>
>>> >>
>>> >>
>>> >> On 22 Feb., 11:38, Nicolas Petton <[hidden email]> wrote:
>>> >> > On 22/02/12 09:45, Zenchess wrote:
>>> >> >
>>> >> > >    Hi Nicolas, I tried your version of the code, and got this:
>>> >> >
>>> >> > > "function WebSocket() { [native code] } does not understand #new"
>>> >> >
>>> >> > Yeah, there's an error in the code I wrote. It should be:
>>> >> > socket := WebSocket newValue: 'ws://www.zenchess.com:4003/broadcast'
>>>
>>> >> >
>>> >> > But anyway WebSocket isn't a normal function, so it doesn't seem to
>>> work
>>> >> > in this particular case :/
>>> >> >
>>> >>
>>> >> I get an error on Chrome 12.0 and FF 10.0 saying:
>>> >>
>>> >>   function WebSocket() { [native code] } does not understand
>>> >> #newValue:"
>>> >>
>>> >> Regards...
>>> >>
>>>
>>> --
>>> Nicolas Petton
>>> http://nicolas-petton.fr
>>>
>>
--
Nicolas Petton
http://nicolas-petton.fr
Reply | Threaded
Open this post in threaded view
|

Re: Amber <> Squeak communication using websockets, round 2

sebastianconcept
In reply to this post by Jacob Wagner
isn't more convenient to use Socket.IO ?





On Monday, February 20, 2012 3:55:29 AM UTC-2, Zenchess wrote:
 Hi, a while back I tried using websockets to work with squeak, pharo
and amber, with some limited success.  I can't remember exactly why I
gave up on the idea, but for some reason it didn't work out for me.  I
posted on the newsgroup about my efforts.

  Well, I noticed a forum post from world.st in which WebClient and
WebServer were updated for squeak to use more modern websocket
protocols.  I decided to try them again.  I now have a fully
functional multiplayer game demo coded in amber in client and squeak
for server.  Using this solution I don't think you need to host amber
in any special way - you don't need to host it in node and you don't
need to use the recent one click solution.  Just serve your client
files via apache or whatever, and have a running squeak image on the
same domain (if it's not on the same domain it won't work because of
cross site scripting security blocks on all major browsers).

  First off, let me say that if you try this - you're probably better
off using squeak instead of pharo.  The package WebClient is made for
squeak, and I had trouble getting the pharo version to work in the
past and recently as well.  If you want to try to get it to work in
pharo, you'll need to figure out how to install 'Installer' first, (I
tried installing it and it crashed my image).

  To get this whole thing to work, get squeak and install the latest
WebClient package (it supports websockets and comes with WebServer
class as well).  In amber no special libraries are necessary, at least
on chrome.  (It should work in firefox and other major browsers as
well).

  Once you've got your squeak image running, it's a simple matter of
using standard WebSocket object in amber/javascript and modifying the
WebSocket class >> example method in squeak.

  In case you have problems getting started, here is my cheat sheet
for useful methods on both sides:


  Amber:

<socket = new WebSocket("ws://www.zenchess.com:4003/broadcast");>.

^^ modify the above code to point to your web domain and the port that
WebSocket example is running on.

<socket.onopen = function(){
            alert("Socket has been opened!");
        self._onOpen();
        }  >.

^^ There's the function you register with your socket object that gets
called when the socket is opened for the first time.  self._onOpen();
is javascript code that will call the onOpen method in the class that
it is running from.  This lets me write an onOpen method in complete
smalltalk.  I couldn't figure out how to convert the above javascript
code into the smalltalk equivalent, so that is why I am using the < >
's .


<socket.onmessage = function(msg){

        data = msg.data;>.


Hopefully the above code and explanation can help anyone that is
looking for amber>regular smalltalk image communication.  I know there
are other solutions but websocket is actually a really good one, since
it's 2-way and does not have to continuously poll to get updates.  By
the way there are some JSON convenience methods for decoding and
encoding JSON in squeak in the WebUtils package of WebClient that came
in handy as well.

  I've been coding a multiplayer turn based strategy game in Ludus/
Amber/Squeak and it's coming along nicely, I'll be sure to demo it
here first. :P