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.
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())
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
Code: Select all
WebSocket_Server::Frame_Text_Send(*Server, *Client, "Hello Client!")
Code: Select all
Free(*Server)