It is currently Tue Mar 09, 2021 7:22 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Websocket Client
PostPosted: Wed Dec 02, 2015 2:21 pm 
Offline
New User
New User

Joined: Sun Jun 24, 2012 6:25 pm
Posts: 8
I've developed an websocket client.

This is not a SERVER, this is a CLIENT who connect to a server.

I've developed it to query an external API/Service running on a websocket-server.

It's not fully tested, and misses some features (look TODO in source), but works with the testserver at echo.websocket.org.

https://gist.github.com/Netzvamp/8623def14501de15c9e4

My question to improve it further:
Is there an easy way to open an encrypted (TSL/SSL) connections to send raw data? Looks like Win32-API is only for HTTPS, but not usable for websockets and not crossplatform.
I've seen cryptlib, does anyone have experience with this lib? Are there better solutions?


Last edited by Netzvamp on Wed Dec 02, 2015 5:55 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Wed Dec 02, 2015 2:33 pm 
Offline
Always Here
Always Here

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 5294
Location: Germany
Hi,

you know thhese threads:
viewtopic.php?f=13&t=56873
viewtopic.php?f=12&t=61606

Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Wed Dec 02, 2015 2:55 pm 
Offline
New User
New User

Joined: Sun Jun 24, 2012 6:25 pm
Posts: 8
infratec wrote:


Yes, i know them. But these are SERVERS, this is an CLIENT who connect to an server. I've developed it, cause i have an external API-Server/Service based on websocket and i needed to query data with purebasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Sun Aug 26, 2018 2:47 pm 
Offline
New User
New User

Joined: Thu Aug 23, 2018 12:13 am
Posts: 7
Hello, just to notice a little bug: pong answer has to be masked, or the server disconnects you. Great script though, thank you Netzvamp :wink:


Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Fri Oct 30, 2020 7:58 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4876
Location: Lyon - France
Someone have a simple PHP websocket server code who works with this great client ?

I have found this and not understand how use it, because there are also an IP to enter on the server and the client :shock:
https://medium.com/@cn007b/super-simple ... 2cd5893575

Code:
<?php

$address = '0.0.0.0';
$port = 12345;

// Create WebSocket.
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, $address, $port);
socket_listen($server);
$client = socket_accept($server);

// Send WebSocket handshake headers.
$request = socket_read($client, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack(
    'H*',
    sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($client, $headers, strlen($headers));

// Send messages into WebSocket in a loop.
while (true) {
    sleep(1);
    $content = 'Now: ' . time();
    $response = chr(129) . chr(strlen($content)) . $content;
    socket_write($client, $response);
}?>

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Sat Oct 31, 2020 10:18 am 
Offline
Always Here
Always Here

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 5294
Location: Germany
You don't need to enter tne server IP :wink:

0.0.0.0 means that it listens on all interfaces.
Only if you want to restrict it to one interface, you have to change it to the IP of the interface you want to use.
But since a server has fixed IP addresses you only need to do this once.


Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Sat Oct 31, 2020 3:36 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4876
Location: Lyon - France
Waaaahh !!! finaly a good news 8) after all this search without succes :|
So you say to me, normally this two codes above must work ? :shock:
I have try several time without succes yesterday ....
Good...i try another time today, now i know that can works, perhaps i have missing something :wink:

What adress i must write ?
Code:
Http://www.myserver.fr/my/path/index.php
Http://www.myserver.fr
www.myserver.fr

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Sun Nov 01, 2020 8:33 pm 
Offline
Always Here
Always Here

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 5294
Location: Germany
To start the 'server' you have to call
http://www.myserver.fr/my/path/index.php

But yout client needs to communicate with www.myserver.fr port 12345


Top
 Profile  
Reply with quote  
 Post subject: Re: Websocket Client
PostPosted: Sun Nov 01, 2020 8:40 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4876
Location: Lyon - France
Thanks INFRATEC for your precious explanation, that works now 8)
But after several try, i discovered WebSocket lock the loop and wait client request :|
So one time more, it's not what i search to do :|
I want each part can send to other everytime, and the other answer if he want....but apparently, it's not simple in PHP :|
I continue my searchs.....again thanks 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 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