日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php弹幕技术轮询,PHP实现长轮询消息实时推送功能代码

發布時間:2024/9/27 php 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php弹幕技术轮询,PHP实现长轮询消息实时推送功能代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  入口文件index.html

反ajax推送

.send{color:#555;text-align: left;}

.require{color:blue;text-align: right;}

.content_box{text-align: center;margin: 20px;

border: 1px solid #ddd;padding: 20px;}

消息框

1

2

發送

連接

$(function(){

//發送消息

$('#btn_send').click(function(){

var send_text = $('#send_text').val();

if(send_text.length <= 0){

$('#error_tip').html('不能輸入空值');

}else{

send(send_text);

}

});

//按回車鍵發送消息

$('#send_text').on('keyup',function(e){

if(e.keyCode == 13){

$('#btn_send').trigger('click');

}

});

//建立通訊鏈接

$('#btn_link').click(function(){

connect();

var _this = $(this);

_this.attr('disabled',true);

_this.html('已連接');

});

});

//建立通訊連接函數

function connect(){

$('#content_box_title').html($('#username').val()+'的消息窗口');

$.ajax({

data:{'user':$('#username').val()},

url:'ajaxPush.PHP',

type:'get',

timeout:0,

dataType:'json',

success:function(data){

$('#content_box').append('

'+data.msg+'');

connect();

}

});

}

//發送消息函數

function send(massege){

var user =$('#username').val();

$.getJSON('write.php',{'msg':massege,'user':user},function(data){

if(data.sf){

$('#content_box').append('

'+massege+'');

$('#send_text').val('');

}else{

$('#error_tip').html('輸入保存錯誤!');

}

});

}

ajax處理輸入 write.php

/**

* Created by TXM.

* Time: 2017/4/18 13:13

* function:

*/

$filename = dirname(__FILE__).'/data.txt';

$isread_file = dirname(__FILE__).'/isread.txt';

$user = dirname(__FILE__).'/user.txt';

//寫入消息,消息未讀,誰發送的消息

file_put_contents($filename,$_GET['msg']);

file_put_contents($isread_file,'0');

file_put_contents($user,$_GET['user']);

echo json_encode(array('sf'=>true));

長輪詢推送 ajaxPush.php

/**

* Created by TXM.

* Time: 2017/4/18 13:12

* function:

*/

$filename = dirname(__FILE__).'/data.txt';

$isread_file = dirname(__FILE__).'/isread.txt';

$userfile = dirname(__FILE__).'/user.txt';

$get_user = $_GET['user'] == '1'?'2':'1';

$msg='';

while(1){

$msg = file_get_contents($filename);

$isread = file_get_contents($isread_file);

$user = file_get_contents($userfile);

//是對方發送的消息,設置消息已讀,退出循環。

if($isread == '0' && $get_user == $user){

file_put_contents($isread_file,'1');

break;

}

sleep(1);

}

echo json_encode(array('msg'=>$msg));

總結

以上是生活随笔為你收集整理的php弹幕技术轮询,PHP实现长轮询消息实时推送功能代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。