Just starting out? Need help? Post your questions and find answers here.
Dmitry
 
Posts: 2
Joined: Fri Apr 20, 2018 9:27 am

TCP sockets?

by Dmitry Fri Apr 20, 2018 9:37 am

Hi everyone!

Could you please advise if it is possible to have TCP sockets in SB app? Something similar to CreateNetworkServer() and OpenNetworkConnection() in PB?

Best regards,
Dmitry.
poshu
 
Posts: 45
Joined: Mon Feb 24, 2014 11:46 pm

Re: TCP sockets?

by poshu Fri Apr 20, 2018 1:24 pm

Nop, not possible : HTML 5 doesn't support tcp sockets.

What can be done though is to use websocket which is really close. That being said, you can't create a server from an HTML5 page, you'd need a server written in something else; see the great Mozilla page about it, or an example for node js.

Now, here is my websocket module :
Code: Select all
DeclareModule Websocket
   Declare Open(Adress.s,*OnMessageCallback,*OnOpenCallback,*OnErrorCallback,*OnCloseCallback)
   Declare Send(message.s)
   Declare Close()
   Declare IsSupported()
   
EndDeclareModule

Module Websocket
   
   ; Public Procedures
   Procedure Open(Adress.s,*OnMessageCallback,*OnOpenCallback,*OnErrorCallback,*OnCloseCallback)
      Protected Result
      ! websocket = new WebSocket(v_adress);
      ! websocket.onopen = function(evt) { p_onopencallback(evt) };
      ! websocket.onclose = function(evt) { p_onclosecallback(evt) };
      ! websocket.onmessage = function(evt) { p_onmessagecallback(evt) };
      ! websocket.onerror = function(evt) { p_onerrorcallback(evt) };
      
      ! v_result = websocket;
      ProcedureReturn Result
   EndProcedure
   
   Procedure Send(message.s)
      ! websocket.send(v_message);
   EndProcedure
   
   Procedure Close()
      ! websocket.close();
   EndProcedure
   
   Procedure IsSupported()
      Protected Result
      !if (window.WebSocket){
    !v_result=1;
      !};
      ProcedureReturn Result
   EndProcedure
   
EndModule

CompilerIf #PB_Compiler_IsMainFile ; Example
      ; Private Procedures
   Procedure onOpen(evt)
      websocket::Send("something")
   EndProcedure
   
   Procedure onClose(evt)
      Debug "onClose"
   EndProcedure
   
   Procedure onMessage(evt)
      Protected MessageFromWs.s
      ! v_messagefromws = v_evt.data;
      Debug MessageFromWs
   EndProcedure
   
   Procedure onError(evt)
      Debug "onError()"
   EndProcedure
   
   websocket::Open("ws://127.0.0.1:9999/", @onMessage(),@onOpen(),@onError(),@onClose())
CompilerEndIf
Dmitry
 
Posts: 2
Joined: Fri Apr 20, 2018 9:27 am

Re: TCP sockets?

by Dmitry Sat Apr 21, 2018 6:44 am

Thanks a lot Poshu! I'll check it out.

Best regards,
Dmitry.
Return to Coding Questions

Who is online

Users browsing this forum: No registered users and 1 guest