dsh102123 2014-05-30 13:18
浏览 2128
已采纳

每1秒发送一次WebSocket消息

I've been tasked settings up the following.

6 individual stopwatches which all sync with each other. They all need to be able to start at the same time They need to be able to be stopped separately (6 people will be racing, when one finished, their timer will stop).

I'm thinking Web Sockets would be a good idea for this, so the timer can sync with the server, and I've followed this to set it up and get it running.

I'm currently at the stage where I can connect 6 individuals, and one admin page to display how many clients their are, and a "Start Timer" button.

I've created this method which i thought would send the current server time to each client every second (for 10 seconds). But it doesn't work, it sends all 10 messages at once, after 10 seconds.

private function startTimer() {
    for($i = 0;$i < 10;$i++) {
        $now = date('H:i:s d-m-Y');
        foreach($this->all as $client) {
            $client['conn']->send(json_encode(array('action' => 'time', 'time' => $now)));
        }
        sleep(1);
    }
}

So, 1) Are Web Sockets a good way to do this, or, is Javascript/PHP not ideal for doing this project? And 2) If Web Sockets will work, how can I keep each client in sync with each other?

  • 写回答

1条回答 默认 最新

  • dpb42021 2014-06-04 08:14
    关注

    Rather than doing a send every second when I pressed start I sent each client the current server time, then get them calculate time passed in Javascript. I realize this may not be the safest solution, but this is a just a little project to be used internally, so it won't be exploited.

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

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题