It is currently Fri Jan 19, 2018 12:54 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: WebSocket Server
PostPosted: Sun Feb 08, 2015 7:24 pm 
Offline
User
User
User avatar

Joined: Sat Jan 12, 2008 11:50 pm
Posts: 38
Location: Hessen, Germany
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:
*Server = WebSocket_Server::Create(8090, @WebSocket_Event())

Receive events as callback:
Code:
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)
     
      Select *Event_Frame\Opcode
        ; #### 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.
      EndSelect
     
  EndSelect
EndProcedure

Send a text-frame:
Code:
WebSocket_Server::Frame_Text_Send(*Server, *Client, "Hello Client!")

Close and free a WebSocket-Server:
Code:
Free(*Server)


Files:

Have fun with it.

_________________
Website


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Mon Feb 09, 2015 12:14 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 21, 2011 9:11 am
Posts: 580
Location: France
Thanks for sharing. The question is : How to install the server in a hosting company?

_________________

➽ Windows 10 - PB 5.45 LTS & PB 5.60

http://falsam.com & EasySprite.js
http://purebasic.chat - Code PureBasic
Image Pure Basic Francophone Community

Sorry for my bad english and the Dunning–Kruger effect.


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Mon Feb 09, 2015 12:52 am 
Offline
User
User
User avatar

Joined: Sat Jan 12, 2008 11:50 pm
Posts: 38
Location: Hessen, Germany
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.

_________________
Website


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Mon Feb 09, 2015 11:31 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13183
Location: France
This is great code, thanks for sharing !


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Mon Feb 09, 2015 3:30 pm 
Offline
Moderator
Moderator
User avatar

Joined: Sat Apr 26, 2003 1:11 am
Posts: 1377
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


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Mon Feb 09, 2015 5:22 pm 
Offline
User
User
User avatar

Joined: Sat Jan 12, 2008 11:50 pm
Posts: 38
Location: Hessen, Germany
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?

_________________
Website


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Tue Feb 10, 2015 11:57 am 
Offline
User
User
User avatar

Joined: Mon Mar 08, 2010 9:14 pm
Posts: 82
Location: "Mens sana in corpore sano"
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


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Tue Feb 10, 2015 1:24 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue May 28, 2013 10:51 pm
Posts: 503
Location: Europe
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/


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Thu Feb 12, 2015 7:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 22, 2010 1:50 am
Posts: 775
Location: Bradenton, FL
It's beautiful!


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Tue May 26, 2015 12:17 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Aug 15, 2009 6:59 pm
Posts: 1217
Thank for the code.
Will you add TSL?


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Tue May 26, 2015 2:18 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 2:49 pm
Posts: 508
It is based on the synchronous pb functions? or did you use the api and go asynchronous?


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Wed Jun 17, 2015 3:32 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 19, 2010 3:42 am
Posts: 431
Downloaded, started PB, startet HTML...
...just worked :D

Thanks a lot for this great code!!!!


Top
 Profile  
Reply with quote  
 Post subject: PB 5.50 b1; WebSocket Server; fingerPrint function;
PostPosted: Thu Jun 16, 2016 2:04 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 19, 2010 3:42 am
Posts: 431
WebSocket server does not work anymore

Code:
; #### 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!


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Thu Jun 16, 2016 2:07 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 19, 2010 3:42 am
Posts: 431
o.k. solved...

Code:
 Temp_SHA1.s = Fingerprint(*Temp_Data, Temp_String_ByteLength, #PB_Cipher_CRC32)


Top
 Profile  
Reply with quote  
 Post subject: Re: WebSocket Server
PostPosted: Thu Jun 16, 2016 2:55 pm 
Offline
User
User
User avatar

Joined: Sat Jan 12, 2008 11:50 pm
Posts: 38
Location: Hessen, Germany
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/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss. But i haven't tried it.

_________________
Website


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye