Install the official swoole demo code to build a websocket. server code: ws_server.php {code ...} client code: index.html?code ..htm I use the command to open the ws_server.php service terminal. The client file index.html is placed under the apache web root directory. I access the local file... install the official swoole demo code to build a websocket,
Server code: ws_server.php
// Create a websocket Server Object and listen to port 0.0.0.0: 9502 $ ws = new swoole_websocket_server ("0.0.0.0", 9502 ); // listen to the WebSocket connection opening event $ ws-> on ('open', function ($ ws, $ request) {var_dump ($ request-> fd, $ request-> get, $ request-> server); $ ws-> push ($ request-> fd, "hello, welcome \ n ");}); // listen to WebSocket message events $ ws-> on ('message', function ($ ws, $ frame) {echo "Message: {$ frame-> data} \ n "; $ ws-> push ($ frame-> fd, "server: {$ frame-> data }");}); // listen to the WebSocket connection close event $ ws-> on ('close', function ($ ws, $ fd) {echo "client-{$ fd} is closed \ n" ;}); $ ws-> start ();
Client code: index.html
《script》var wsServer = 'ws://127.0.0.1:9502';var websocket = new WebSocket(wsServer);websocket.onopen = function (evt) { console.log("Connected to WebSocket server.");};websocket.onclose = function (evt) { console.log("Disconnected");};websocket.onmessage = function (evt) { console.log('Retrieved data from server: ' + evt.data);};websocket.onerror = function (evt, e) { console.log('Error occured: ' + evt.data);};《script》
I started the ws_server.php service with a command, and put the client file index.html under the apache web root directory. I accessed localhost and saw the "Connected to WebSocket server." message on the console, proving that the access was successful.
However, if I change the ip address of the ws_server.php listener to another ip address (for example, 192.168.0.233: 9502133, then change the code related to index.html to wsServer = 'ws: // 192.168.0.233: 100 ';
Why is access unsuccessful?
Reply content:
Install the official swoole demo code to build a websocket,
Server code: ws_server.php
// Create a websocket Server Object and listen to port 0.0.0.0: 9502 $ ws = new swoole_websocket_server ("0.0.0.0", 9502 ); // listen to the WebSocket connection opening event $ ws-> on ('open', function ($ ws, $ request) {var_dump ($ request-> fd, $ request-> get, $ request-> server); $ ws-> push ($ request-> fd, "hello, welcome \ n ");}); // listen to WebSocket message events $ ws-> on ('message', function ($ ws, $ frame) {echo "Message: {$ frame-> data} \ n "; $ ws-> push ($ frame-> fd, "server: {$ frame-> data }");}); // listen to the WebSocket connection close event $ ws-> on ('close', function ($ ws, $ fd) {echo "client-{$ fd} is closed \ n" ;}); $ ws-> start ();
Client code: index.html
《script》var wsServer = 'ws://127.0.0.1:9502';var websocket = new WebSocket(wsServer);websocket.onopen = function (evt) { console.log("Connected to WebSocket server.");};websocket.onclose = function (evt) { console.log("Disconnected");};websocket.onmessage = function (evt) { console.log('Retrieved data from server: ' + evt.data);};websocket.onerror = function (evt, e) { console.log('Error occured: ' + evt.data);};《script》
I started the ws_server.php service with a command, and put the client file index.html under the apache web root directory. I accessed localhost and saw the "Connected to WebSocket server." message on the console, proving that the access was successful.
However, if I change the ip address of the ws_server.php listener to another ip address (for example, 192.168.0.233: 9502133, then change the code related to index.html to wsServer = 'ws: // 192.168.0.233: 100 ';
Why is access unsuccessful?
Set0.0.0.0
Listen to connections from all address sources, so you can connect.
And set192.168.0.233
Accept from192.168.0.233
This host connection, your local host is not192.168.0.233
The connection cannot be established.