PHP websocket server and JavaScript connection - Stack Overflow

I have created a PHP websocket server script as following.$socket = socket_create(AF_INET, SOCK_STREAM,

I have created a PHP websocket server script as following.

$socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_bind($socket, '127.0.0.1', 5001);
while (true) {
    $result = socket_listen($socket);
    $client =  socket_accept($socket);
    $input = socket_read($client, 1024);
    $output = 'Input Received: '.$input;
    socket_write($client, $output, strlen($output));
    socket_close($client);
}
socket_close($socket);

I've executed file containing above code in terminal using following mand

$ php server.php

Now I want to get the response on my front-end using JavaScript. I've used following JS snippet but not working.

var ws = new WebSocket("ws://localhost:5001/echo");
ws.onopen = function() {
ws.send("Message to send");
    alert("Message is sent...");
};
ws.onmessage = function (evt) { 
    var received_msg = evt.data;
    alert("Message is received...");
};
ws.onclose = function() { 
    alert("Connection is closed..."); 
};

Receiving following error:

WebSocket connection to 'ws://localhost:5001/echo' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE

I have created a PHP websocket server script as following.

$socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_bind($socket, '127.0.0.1', 5001);
while (true) {
    $result = socket_listen($socket);
    $client =  socket_accept($socket);
    $input = socket_read($client, 1024);
    $output = 'Input Received: '.$input;
    socket_write($client, $output, strlen($output));
    socket_close($client);
}
socket_close($socket);

I've executed file containing above code in terminal using following mand

$ php server.php

Now I want to get the response on my front-end using JavaScript. I've used following JS snippet but not working.

var ws = new WebSocket("ws://localhost:5001/echo");
ws.onopen = function() {
ws.send("Message to send");
    alert("Message is sent...");
};
ws.onmessage = function (evt) { 
    var received_msg = evt.data;
    alert("Message is received...");
};
ws.onclose = function() { 
    alert("Connection is closed..."); 
};

Receiving following error:

WebSocket connection to 'ws://localhost:5001/echo' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE

Share Improve this question edited Aug 6, 2018 at 7:42 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Aug 6, 2018 at 7:34 Pramod KumarPramod Kumar 1061 gold badge2 silver badges7 bronze badges 5
  • 3 You've created a TCP socket, not a websocket. There's a fair bit more to it than that. – Ben Fortune Commented Aug 6, 2018 at 7:40
  • @BenFortune - how to create websocket (ws) rather than tcp socket? or tcp socket able to fulfil my requirement? – Pramod Kumar Commented Aug 6, 2018 at 7:58
  • 1 There are plenty of libraries for it.stackoverflow./questions/12203443/… – Ben Fortune Commented Aug 6, 2018 at 8:10
  • @BenFortune - What can I do to make JavaScript municate with TCP rather than creating websocket using 3rd party libraries. – Pramod Kumar Commented Aug 6, 2018 at 9:14
  • 3 Nothing. Browsers don't generally support raw TCP connections using JS. You need an abstraction, which is why websockets exist. – Ben Fortune Commented Aug 6, 2018 at 9:17
Add a ment  | 

1 Answer 1

Reset to default 2

Writing a PHP websocket server requires to perform a certain handshake with the client connecting to you via ws:// . In addition you need to decode and encode messages going in and out. The real fun starts when the clients contact you via wss://, then you have to take care of certificate and key within your PHP server and use stream-io instead of socket-io and so on and on. You can have a look at my implementation of all this and use it as another starting point .

https://github./napengam/phpWebSocketServer

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745235089a4617852.html

相关推荐

  • PHP websocket server and JavaScript connection - Stack Overflow

    I have created a PHP websocket server script as following.$socket = socket_create(AF_INET, SOCK_STREAM,

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信