I used swoole to build a websocket service. I have some questions about IP address access.

Source: Internet
Author: User
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.0Listen to connections from all address sources, so you can connect.
And set192.168.0.233Accept from192.168.0.233This host connection, your local host is not192.168.0.233The connection cannot be established.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.