Just starting out? Need help? Post your questions and find answers here.
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,
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()

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
   Procedure Send(message.s)
      ! websocket.send(v_message);
   Procedure Close()
      ! websocket.close();
   Procedure IsSupported()
      Protected Result
      !if (window.WebSocket){
      ProcedureReturn Result

CompilerIf #PB_Compiler_IsMainFile ; Example
      ; Private Procedures
   Procedure onOpen(evt)
   Procedure onClose(evt)
      Debug "onClose"
   Procedure onMessage(evt)
      Protected MessageFromWs.s
      ! v_messagefromws = v_evt.data;
      Debug MessageFromWs
   Procedure onError(evt)
      Debug "onError()"
   websocket::Open("ws://", @onMessage(),@onOpen(),@onError(),@onClose())
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,
Return to Coding Questions

Who is online

Users browsing this forum: No registered users and 2 guests