amphp / websocket-server

WebSocket component for PHP based on the Amp HTTP server.
MIT License
114 stars 17 forks source link

Accept, GET and instant Close upon connection with the example #17

Closed DrLightman closed 4 years ago

DrLightman commented 4 years ago

(Thank you for fixing the example)

Tried to couple it with a simple index.html client to test a connection to the server, but as soon as I load the client in the browser and the connection is estabilished, the connection is instantly closed:

server log

The simple client's index.html that runs on an Apache vhost in the same machine at http://amphpws.local:8080/index.html, not publicly accessible:

<html>
<head>
<style>
    input, button { padding:10px; }
</style>
</head>
<body>
<input type="text" id="message"/>
<button onclick="transmitMessage()">Send</button>
<script>
    // Create a new WebSocket.
    var socket = new WebSocket('ws://127.0.0.1:1337');

    // Define the message
    var message = document.getElementById('message');

    function transmitMessage() {
        socket.send(message.value);
    }

    socket.onmessage = function (e) {
        alert(e.data);
    }
</script>
</body>
</html>

The result in the Chrome browser:

client page

The connection is closed as soon as I load the page so I can't even test in order to "Send" some message.

DrLightman commented 4 years ago

And as soon as I opened the issue, I realized that maybe I had to use ws://127.0.0.1:1337/broadcast in the client's websocket url, not plain ws://127.0.0.1:1337?

kelunik commented 4 years ago

Yup, see https://github.com/amphp/websocket-server/blob/ed408970287bd710a35f1d110005a18c39dab987/examples/broadcast-server/public/index.html#L84.