WebSocket Server

Share your advanced PureBasic knowledge/code with the community.
User avatar
Dadido3
User
User
Posts: 52
Joined: Sat Jan 12, 2008 11:50 pm
Location: Hessen, Germany
Contact:

WebSocket Server

Post by Dadido3 »

Hi,

Here's a WebSocket-Server module i wrote over the last days.
It can be used to establish a continuous, bidirectional connection between a purebasic program and javascript running in a website.

Tested OSes:
  • Windows 7 x64
  • Others should also work.
Example:
The repository contains a chat-application as an example.
The server is written in PureBasic and the client in HTML and JavaScript.
I have currently running a server and the client should automatically connect to it. Click here to open the client.

Open a WebSocket-Server:

Code: Select all

*Server = WebSocket_Server::Create(8090, @WebSocket_Event())
Receive events as callback:

Code: Select all

Procedure WebSocket_Event(*Server, *Client, Event, *Event_Frame.WebSocket_Server::Event_Frame)
  Select Event
    Case WebSocket_Server::#Event_Connect
      PrintN(" #### Client connected: " + *Client)
      
    Case WebSocket_Server::#Event_Disconnect
      PrintN(" #### Client disconnected: " + *Client)
      ; !!!! From the moment you receive this event the *Client can not be used anymore !!!!
      
    Case WebSocket_Server::#Event_Frame
      PrintN(" #### Frame received from " + *Client)
      
      ; #### OpCode is the type of frame you receive.
      ; #### Either Text, Binary-Data, Ping-Frames or other stuff.
      ; #### You only need to care about text and binary frames.
      Select *Event_Frame\Opcode
        Case WebSocket_Server::#Opcode_Ping
          Debug "Client sent a ping frame"
        Case WebSocket_Server::#Opcode_Text
          Debug "Text received: " + PeekS(*Event_Frame\Payload, *Event_Frame\Payload_Size, #PB_UTF8|#PB_ByteLength)
        Case WebSocket_Server::#Opcode_Binary
          ; *Event_Frame\Payload contains the data, *Event_Frame\Payload_Size is the size of the data in bytes
      EndSelect
      
  EndSelect
EndProcedure
Send a text-frame:

Code: Select all

WebSocket_Server::Frame_Text_Send(*Server, *Client, "Hello Client!")
Close and free a WebSocket-Server:

Code: Select all

Free(*Server)
Files: Have fun with it.
Last edited by Dadido3 on Wed Feb 28, 2018 8:21 pm, edited 1 time in total.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: WebSocket Server

Post by falsam »

Thanks for sharing. The question is : How to install the server in a hosting company?

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
Dadido3
User
User
Posts: 52
Joined: Sat Jan 12, 2008 11:50 pm
Location: Hessen, Germany
Contact:

Re: WebSocket Server

Post by Dadido3 »

falsam wrote:How to install the server in a hosting company?
Well, it wouldn't work on a shared webserver just for webhosting.
You would atleast need a VPS (Virtual Private Server) with linux or windows, but they aren't much more expensive than a shared webserver.
If you have something like that you just need to run your purebasic application with the WebSocket-Server on it.

Or you could use a dynamic dns and run the WebSocket-Server at home...

But there are also some usecases where you don't need to rent a server and can run it locally:
  • Create a webinterface for your software. You can access the webinterface from any mobile device with a modern webbrowser.
  • Communicate with Websites in the WebGadget of purebasic. You could send data to it and it would render nice charts in realtime. (e.g. via http://d3js.org/)
  • And other things.
Fred
Administrator
Administrator
Posts: 16581
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: WebSocket Server

Post by Fred »

This is great code, thanks for sharing !
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

Re: WebSocket Server

Post by Rings »

windows x64 compiles fine and runs ok so far.

windows x86 compiles with a error in line 437
in module websocket_server.pbi:

The specified number is incorrect for '<<' or '>>' (must be 0-31)

(BITSHIFTING !!)
SPAMINATOR NR.1
User avatar
Dadido3
User
User
Posts: 52
Joined: Sat Jan 12, 2008 11:50 pm
Location: Hessen, Germany
Contact:

Re: WebSocket Server

Post by Dadido3 »

Hi,

yeah i should have tested it under x86...
It's fixed, i changed the conversion of the byteorder to use memory operations.
So now it's a little bit faster.

Any other strange stuff which isn't working as expected?
User avatar
holzhacker
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Mar 08, 2010 9:14 pm
Location: "Mens sana in corpore sano"
Contact:

Re: WebSocket Server

Post by holzhacker »

He works on Ubuntu 14.04.1 LTS (64-bit), compiled in PureBasic 5.31 (x64).

Congratulations !!! 8)
Greetings and thanks!

Romerio Medeiros
romerio@gmail.com
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: WebSocket Server

Post by bbanelli »

Should change Executable format to Console to work on OS X.

Image

Great job Dadido3, very neat usage of all PB's resources and abilities!
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: WebSocket Server

Post by RichAlgeni »

It's beautiful!
Thorium
Addict
Addict
Posts: 1271
Joined: Sat Aug 15, 2009 6:59 pm

Re: WebSocket Server

Post by Thorium »

Thank for the code.
Will you add TSL?
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: WebSocket Server

Post by Justin »

It is based on the synchronous pb functions? or did you use the api and go asynchronous?
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: WebSocket Server

Post by HanPBF »

Downloaded, started PB, startet HTML...
...just worked :D

Thanks a lot for this great code!!!!
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

PB 5.50 b1; WebSocket Server; fingerPrint function;

Post by HanPBF »

WebSocket server does not work anymore

Code: Select all

; #### Generate the SHA1
 *Temp_Data_2 = AllocateMemory(20)
Temp_SHA1.s = Fingerprint(*Temp_Data, Temp_String_ByteLength, #PB_Cipher_SHA1) ; error: The specified cipher plugin is missing
 For i = 0 To 19
  PokeA(*Temp_Data_2+i, Val("$"+Mid(Temp_SHA1, 1+i*2, 2)))
Next
Error: "The specified cipher plugin is missing"

There are some hints to the fingerprint function in this forum; but I could not puzzle it out.
Is another function needed? Which use*Fingerprint() needs to be used?


Any help?


Thanks a lot in advance!

Regards!
HanPBF
Enthusiast
Enthusiast
Posts: 562
Joined: Fri Feb 19, 2010 3:42 am

Re: WebSocket Server

Post by HanPBF »

o.k. solved...

Code: Select all

 Temp_SHA1.s = Fingerprint(*Temp_Data, Temp_String_ByteLength, #PB_Cipher_CRC32)
User avatar
Dadido3
User
User
Posts: 52
Joined: Sat Jan 12, 2008 11:50 pm
Location: Hessen, Germany
Contact:

Re: WebSocket Server

Post by Dadido3 »

Hi,

it would be strange if it works with CRC32. It may compile, but you shouldn't be able to connect.
Beside that it's easier to use StringFingerprint(...) as you can define the string encoding there.

Even though i didn't mentioned it, i updated the include about 8 months ago.
The easiest way is just to use the new version, as it is compatible with the newest PB version.
Justin wrote:It is based on the synchronous pb functions? or did you use the api and go asynchronous?
I don't know if you still care, but it's using PB's built in network functions. But the network code is running in its own thread, so the network logic is only blocked as long as the callback is executed.
If you don't want to serve several thousand clients it should work fine.
Thorium wrote:Will you add TSL?
It's planned, but i can't say when i'll add it.

Edit:
If TLS is needed you can use a webserver as proxy. For NGINX this may be helpful: http://stackoverflow.com/questions/1210 ... le-ssl-wss. But i haven't tried it.
Post Reply