Websocket on localhost does not establish connection

Asked

Viewed 958 times

0

I’m testing WebSocket turning at the door 80, I’ve already disabled the SSL, but I’m not getting results...

javascript

 var ws = new WebSocket('ws://localhost:80/socket2.php');

An example I have seen, tested and successfully returns was using wss://echo.websocket.org. So I guess the mistake is around here somewhere. The return of the browser is always the same:

Firefox was unable to establish a connection to the Ws:/localhost/socket2.php server


The headers as in the example:

socket2.php

header("HTTP/1.1 101 Switching Protocols");

header("Upgrade: websocket");
header("Connection: Upgrade");
header("Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");

On the console the Websocket request returns 101 / websocket / 0B

NOTE: Use Apache, everything is as before.

2 answers

2


Socket is a request that is always open. You need an application running on the other side. PHP is an application that has "start-middle-end". From what you’ve shown you’re trying to connect to the socket by making a PHP call, which you’re instructed to do exactly: Send headers and close.

I strongly recommend not using PHP on the server for sockets. PHP was not made for this. For this specific issue Node would bring you higher performance and lower resource consumption.

1

The server has to stay on a port other than apache, by default it comes on 80, if your apache is on 80, change the server port to another, 8080 for example so there is no conflict, I think this should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.