Jump to content
Sign in to follow this  
Flemming

TCP connection

Recommended Posts

Hi

 

Is it possible to make a TCP connection from the app to a remote server ? I have developed a image-filter software in Lazarus and want to build a app that sends a picture to the server, that then replys with a rendered picture.

Share this post


Link to post
Share on other sites

The easiest way would be to use a normal http request for it (to the webserver).

Web clients (in a browser) cannot do lower level tcp connections to any server so it should always be done using http.

 

A more fancy way would be to use websockets (only for html5 browsers and devices!): this is tcp over http. However, for example android 2.3 does not support websockets...

(but the socket.io library can fallback to normal http, this lib works on top of ws and http).

Anyway, you will need a webserver

Share this post


Link to post
Share on other sites

Thanks for your reply Andre !

 

I think websocket most be the way to go. I have tryed Bauglir Websocket 2 compiled in lazarus, and it seems to work (just have to solve a double router problem for WAN connection).

 

But how do i create a client in SMS ?

Share this post


Link to post
Share on other sites

I made some automatic generation of wrappers for websocket and socket.io (works on top of http and websockets) some time ago, maybe you can try these:

https://drive.google.com/folderview?id=0B5AcQp14Q5FaX2xiSGV5ejJHVHM

 

In lib: search for JWebSockets

And for socket.io, I included the client side .js too

 

About how to use them: you can read about how websockets work here (works almost identical in smart, using onopen and onmessage):

http://www.html5rocks.com/en/tutorials/websockets/basics/

http://www.websocket.org/

 

Socket.io works much nices (less low level, with auto reconnect etc) but you need Node.js on the serverside for it (node.js in Smart will be in a next release). I'm working on an open source Delphi server but nothing released yet

Share this post


Link to post
Share on other sites

Thanks very much for the files. I made a copy to the 'Librarys' folder and i guess that i have to add the socket.io.socket.io to the uses clause. But the files don't give me any clue on how to setup a client connection. Could you please give me a little code eksemple on a client ? Maybe i can go from there ;-)

Share this post


Link to post
Share on other sites

sorry for the delay

it seems the lib.core.pas contains some errors (it was auto generated...)

When you change the following lines:

 

JWebSocket = class external 'WebSocket' (JEventTarget)

public

constructor Create(url: string = ''); overload;

 

You can then use the following code:

 

//var ws := new JWebSocket(); //if same server

var ws := new JWebSocket('ws://localhost:8090'); //other server

ws.onopen :=

function(ev: JEvent): variant

begin

ws.send('test');

end;

 

ws.onmessage :=

function(ev: variant): variant

begin

Console.WriteLn('Message received: ' + ev);

end;

 

Share this post


Link to post
Share on other sites

Ok, i changed lib.Core.pas (line 6915) to:

 

JWebSocket = class external 'WebSocket'(JEventTarget)

public

constructor Create(url: string = ''); overload;

 

 

I added lib.Core to the uses clause.

I asume the code could be somthing like this:

 

ws : JWebSocket;

function WSOnOpen(ev: JEvent): variant;

function WSOnMessage(ev: variant): variant;

 

function TMainForm.WSOnOpen(ev: JEvent): variant;

begin

ws.send('test');

end;

 

function TMainForm.WSOnMessage(ev: variant): variant;

begin

memo1.text:='Got message: '+ev;

end;

 

procedure TMainForm.Bt_ConnectClick(Sender: TObject);

begin

ws := new JWebSocket();

ws.onopen :=WSOnOpen;

ws.onmessage :=WSOnMessage;

end;

 

When compiling i get:

 

[iDE] Internal error. Code-generator threw exception ECodeGenException with message External symbol "JWheelEvent" already defined

 

[iDE] Compilation successfull [3 seconds,385 msec.]

 

I tried to remove all code beside the lib.core in the uses clause, and i got the same compiler-message ?

 

Share this post


Link to post
Share on other sites

yesm there is some stuff already defined in other w3xyz units, so for the mean time, you can put this in a seperate unit:

 

//uses

// lib.Core;

 

type

JEvent = class external;

 

JCloseEvent = class external(JEvent)

public

procedure initCloseEvent(typeArg: string; canBubbleArg: boolean; cancelableArg: boolean; wasCleanArg: boolean; codeArg: integer; reasonArg: string);

property wasClean: boolean;

property reason: string;

property code: integer;

end;

JCloseEvent_Core = class external "CloseEvent"

public

function &new(): JCloseEvent;

property prototype: JCloseEvent;

end;

 

JErrorEvent = class external(JEvent)

public

procedure initErrorEvent(typeArg: string; canBubbleArg: boolean; cancelableArg: boolean; messageArg: string; filenameArg: string; linenoArg: integer);

property colno: integer;

property filename: string;

property lineno: integer;

property message: string;

end;

JErrorEvent_Core = class external "ErrorEvent"

public

function &new(): JErrorEvent;

property prototype: JErrorEvent;

end;

 

JEventListener = class external "Object"

end;

 

JEventTarget = class external "Object"

public

procedure removeEventListener(&type: string; listener: JEventListener; useCapture: boolean);overload;

procedure addEventListener(&type: string; listener: JEventListener; useCapture: boolean);overload;

function dispatchEvent(evt: JEvent): boolean;

end;

 

TWebSocket_onopen_ = function (ev: JEvent): variant;

TWebSocket_onmessage_ = function (ev: variant): variant;

TWebSocket_onclose_ = function (ev: JCloseEvent): variant;

TWebSocket_onerror_ = function (ev: JErrorEvent): variant;

 

JWebSocket = class external 'WebSocket' (JEventTarget)

public

constructor Create(url: string = ''); overload;

 

procedure close(code: integer);overload;

procedure close(code: integer;reason: string);overload;

procedure send(data: variant);

property protocol: string;

property readyState: integer;

property bufferedAmount: integer;

property onopen: TWebSocket_onopen_;

property extensions: string;

property onmessage: TWebSocket_onmessage_;

property onclose: TWebSocket_onclose_;

property onerror: TWebSocket_onerror_;

property binaryType: string;

property url: string;

property OPEN: integer;

property CLOSING: integer;

property CONNECTING: integer;

property CLOSED: integer;

end;

JWebSocket_Core = class external "WebSocket"

public

function &new(url: string): JWebSocket;overload;

function &new(url: string; prototcol: string): JWebSocket;overload;

function &new(url: string; prototcol: array of string): JWebSocket;overload;

property prototype: JWebSocket;

property OPEN: integer;

property CLOSING: integer;

property CONNECTING: integer;

property CLOSED: integer;

end;

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×