Reputation: 2777
I want to implement a simple WebSocket server using PHP and nginx, and am under the following impression:
With this in mind, I tried the following simple implementation consisting of a JavaScript client, an nginx configuration, and a PHP server (see below).
Results:
Connection: upgrade
, Sec-WebSocket-Key: ***
and similar fields. I assume this is good.onopen
event is never triggered on the client, and this is where I'm stuck.Questions:
The Javascript client:
function socket() {
var ws = new WebSocket("ws://socket/hello/");
ws.onopen = function() {
ws.send("Hello, World!");
};
ws.onmessage = function(event) {
console.log(event.data);
};
}
socket();
The nginx configuration:
server {
location /hello/ {
proxy_pass http://localhost:10000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
server_name socket;
root socket;
}
The PHP server:
(Implemented exactly as described in Example #1 in the PHP manual, only changing $address
to 127.0.0.1
.)
Upvotes: 1
Views: 1301
Reputation: 19771
It can, but only if the socket server implements the websocket protocol. If you are asking if it can be used as a generic TCP or UDP socket, it cannot. Full stop.
Upvotes: 2