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:
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 5WebSocket connection to 'ws://localhost:5001/echo' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
- 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
1 Answer
Reset to default 2Writing 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
评论列表(0条)