douzhi3667 2017-01-05 16:47
浏览 38
已采纳

从过去的Ratchet websocket服务器使用的解绑端口

I had a server script running and I closed it prematurely using <kbd>Ctrl</kbd>+<kbd>Z</kbd>. When I tried to run it again it tells me that the port 1001 is already in use. In a live environment there is always the risk that the script could crash or something else may trigger it to end early. How do I automatically unbind this port before connecting to it? I am going to use a port that I know nothing else will ever use, so its safe to unbind it automatically.

Currently my server script is basic.

    $server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()
        )
    ),
    1001
);

$server->run();

I followed the guide found here http://socketo.me/docs/hello-world . Everything has worked as expected, I'mm just stuck with this issue now.

I am running PHP 5.4 on Centos 7.

  • 写回答

1条回答 默认 最新

  • douba1214 2017-01-11 10:33
    关注

    I couldnt find any way to do this through the command line. Only solution i could see, which could be trouble if someone finds out is to kill the script by sending a message to the websocket. Every message to the websocket server has an "action". A switch will then do something for each action. Create a random action thats impossible to guess. Within this switch use die();. This will kill the script and unbind the port so you dont need to go finding the PID and killing that manually when the server needs restarted.

    This can be used to make a quick restart .sh script. Use php to make a websocket call to kill the server and call if from a bash script, then call php server.php and away you go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我知道什么是混合树,但是怎么写代码啊
  • ¥50 开发板linux系统安装dpkg,apt函数库 有偿
  • ¥15 浏览器时间循环 交互事件和延时事件的 优先级与执行问题
  • ¥15 GD模块安装出错,libgd无法正常安装
  • ¥20 求有缘人帮我把笛卡尔坐标系转换为经纬度 有偿
  • ¥15 vue2中使用计算属性
  • ¥50 远程桌面打开Mastercam、没有许可证、物理机打开正常
  • ¥15 ubuntu安装gdal后java读取tif文件报错
  • ¥15 请问lammps怎么做两种金属连接的原子浓度分布图
  • ¥15 求jacquard数据集