Page 1 of 1

Compression & Network libraries

Posted: Sat Feb 15, 2020 12:01 pm
by kingwolf71
I am new to the spiderbasic world; but old timer with PureBasic
The compression and network libraries are a sore miss
Is there a timeline of these features ever being part of SB ?

Re: Compression & Network libraries

Posted: Thu Feb 20, 2020 3:30 am
by poshu
There won't be a PB compatible network library : HTML5 can't use traditional socket and is limited to websocket support.

That being said, here is my websocket module for SpiderBasic

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
   Procedure onOpen(evt)
      websocket::Send("Ababa")
   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:8765/", @onMessage(),@onOpen(),@onError(),@onClose())
CompilerEndIf


And you can find a (very clean) websocket server written by David Vogel for PureBasic right there : https://github.com/Dadido3/WebSocket_Server .

I've been using this pair for years in a few of my projects (including an artistic installation with hundreds users connected at once), so I can vouch for its stability.