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 Selenium+docker Chrome不能运行
  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥50 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图