TCPSocket into Webapp

Hi all.

I’m using TPCSocket class into my WebApp.
I created a class with a method calling my TCPSockets
But it doesn’t works perfectly.
Indeed, i remembered that i need to use an event : DataAvailable

But how to declare this event for using?

I started my code with this:

[code] Dim TCPSocket1 As TCPSocket
Const time_out = 200000
Dim mStart as Double = Microseconds
Dim Result, CrLf as String

CrLf = EndOfLine.Windows
TCPSocket1 = new TCPSocket

TCPSocket1.Port = App.ami_port
TCPSocket1.Listen
TCPSocket1.Address = App.elx_host
TCPSocket1.Connect

do
TCPSocket1.Poll
loop until (TCPSocket1.IsConnected) or (Microseconds - mStart > time_out)
…/…
[/code]

Have you got any idea please?

It’s a pity that there’s not tcpsocket into the framework menu under webapp.
With it, i could have this event natively :confused:

Regards

You can add a TCPSocket to a web page. But anyhow, I use multiple TCPsockets in my web app. The secret for DataAvailable is the AddHandler directive.

TCPSockets are not subclassed from the Control superclass. Therefore, you can instantiate them in code. There’s a LOT of reasons to do this. For example, I have an object in code that represents a real world object I connect to over a telnet connection. That object is not a control on a page but a collection of methods, properties, events, etc. The TCPSocket is just one of them.

So, to add the DataAvailable event you simply create a method like this on your page or wherever:

Sub TCPSock_DataAvaialable_Handler(t as TCPSocket)

// Put your code in here

End Sub

Then in the constructor method of your page or item or whatever you would do:

AddHandler TCPSocket1.DataAvailable, AddressOf TCPSock_DataAvailable_Handler

Now, the framework will use that method as the handler for the DataAvailable event.

The other thing you could do is create a custom subclass of the TCPSocket and in its DataAvailable event handler, you could put all your code.

Lots of ways to skin this cat.

Ho Jon.
Thanks for these explanations. :slight_smile:
It’s nice of you.

I’ll try to do it.

Just to be sure. another question.

This is the best way to read the result of the sending order .

For example in my code i’ve this.

TCPSocket1.Write ("Action: login" + CrLf) TCPSocket1.Poll Result = TCPSocket1.ReadAll (Encodings.ASCII)
Result should take the responce of the Write’s content, right?

Because, my code takes a result sometimes yes, sometimes no.

I compared with an old project under RealBasic (desktop), and all works fine.
I would like to be sure that under Xojo this is the same way under webapp.

Well, first of all there’s a couple of things you’ll want to think about with the web app:

1.) This TCP socket should really be part of the APP object and not a page. The reason is that you probably can’t have a page opening TCPSockets. The APP object can communicate with other things over a TCP socket. All that runs on the server side.

2.) I would not hold that Result will ALWAYS show the contents of the read buffer. The poll command causes the DataAvailable event to fire but it does NOT mean that the data has yet been received by the internal buffers of the socket. You will probably want to put your ReadAll statement in your DataAvailable handler.

Also what is CrLf? I know what you mean by it but Xojo has built in items called EndOfLine that handles that.

And finally, unless the item you are talking to encodes everything in ASCII, I would use UTF8 as that seems to be more common these days. ASCII is a subset of UTF8 if I am not mistaken.

[quote=137474:@Jon Ogden]Well, first of all there’s a couple of things you’ll want to think about with the web app:

1.) This TCP socket should really be part of the APP object and not a page. The reason is that you probably can’t have a page opening TCPSockets. The APP object can communicate with other things over a TCP socket. All that runs on the server side.
[/quote]

No, i don’t use this TCPSocket on the page for ever.
I use it only when i click on a button or else.
So i’m calling a class including my method for example:

Mousedwon : .../ Dim AMI As New Class_AMI .../ AMI.asterisk_db_put("cbr ",ListBox_Asterisk.Cell(i,0),"1") .../
Next, my class

[code]Class AMI : Method : asterisk_db_put( arg1, arg2, arg3)

and my code[/code]
So this code is called each once i click on my picture, that’s all.

[quote=137474:@Jon Ogden]Also what is CrLf? I know what you mean by it but Xojo has built in items called EndOfLine that handles that.

And finally, unless the item you are talking to encodes everything in ASCII, I would use UTF8 as that seems to be more common these days. ASCII is a subset of UTF8 if I am not mistaken.[/quote]

Yes, I thought at this issue, When i put :

  • chr(13) + ch(10) or
  • endofline.windows or
  • endofline.unix

I’ve not the same result, but it doesn’t work anyway.

I believe that my project doesn’t send properly these crlf encodings. i guess !!!

You simply aren’t waiting for the communications to finish. Think asynchronous, event driven programming. You’re trying to do this in a synchronous, procedural way.

Indeed Tim, this could be a reason of this issue because i tried to play with a timer between two Writte. and it’s better but not yet.

when i will find successfully a way, i’ll let you know.
But anyway now, i’ve some clues

Yes, now it works.

I created a kind of function like a sleep, and i make a pause between two events.

Result:

[code]Asterisk Call Manager/1.3
Response: Success
Message: Authentication accepted

Event: FullyBooted
Privilege: system,all
Status: Fully Booted

Response: Success
ActionID: cbr
Message: Updated database successfully[/code]

Realy great.
Thanks at all.