I used Swoole to build a websocket service, a little doubt about accessing IP.

Source: Internet
Author: User
Keywords Php swoole websocket
Tags php server
Install Swoole official Demo code to build a WebSocket,
Service-side code: ws_server.php

//创建websocket服务器对象,监听0.0.0.0:9502端口$ws = new swoole_websocket_server("0.0.0.0", 9502);//监听WebSocket连接打开事件$ws->on('open', function ($ws, $request) {    var_dump($request->fd, $request->get, $request->server);    $ws->push($request->fd, "hello, welcome\n");});//监听WebSocket消息事件$ws->on('message', function ($ws, $frame) {    echo "Message: {$frame->data}\n";    $ws->push($frame->fd, "server: {$frame->data}");});//监听WebSocket连接关闭事件$ws->on('close', function ($ws, $fd) {    echo "client-{$fd} is closed\n";});$ws->start();

Client code: index.html

I use the command to open the ws_server.php server, the client file index.html placed in the Apache Web root directory, I visit localhost, the console console can see "Connected to WebSocket server." And so on, to prove that the visit was successful
But if I change ws_server.php's listening IP to other (such as 192.168.0.233:9502), then change the index.html related code to WSServer = ' ws://192.168.0.233:9502 ';
Why did the visit not succeed?

Reply content:

Install Swoole official Demo code to build a WebSocket,
Service-side code: ws_server.php

//创建websocket服务器对象,监听0.0.0.0:9502端口$ws = new swoole_websocket_server("0.0.0.0", 9502);//监听WebSocket连接打开事件$ws->on('open', function ($ws, $request) {    var_dump($request->fd, $request->get, $request->server);    $ws->push($request->fd, "hello, welcome\n");});//监听WebSocket消息事件$ws->on('message', function ($ws, $frame) {    echo "Message: {$frame->data}\n";    $ws->push($frame->fd, "server: {$frame->data}");});//监听WebSocket连接关闭事件$ws->on('close', function ($ws, $fd) {    echo "client-{$fd} is closed\n";});$ws->start();

Client code: index.html

I use the command to open the ws_server.php server, the client file index.html placed in the Apache Web root directory, I visit localhost, the console console can see "Connected to WebSocket server." And so on, to prove that the visit was successful
But if I change ws_server.php's listening IP to other (such as 192.168.0.233:9502), then change the index.html related code to WSServer = ' ws://192.168.0.233:9502 ';
Why did the visit not succeed?

Set to 0.0.0.0 represent a connection that listens to all address sources, so you can connect.
Instead of being set to 192.168.0.233 accept 192.168.0.233 connections from this host, your local host is not 192.168.0.233 , and the connection naturally 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.