免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6265 | 回复: 13
打印 上一主题 下一主题

网页聊天,怎么用php实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-02 10:35 |只看该作者 |倒序浏览
我们公司想做个类似的网页聊天东东,只要能简单的发送接受文字消息,定时向服务器发送心跳包就行。
但是公司的网页试asp做的,php能不能嵌入到asp脚本中实现上面的功能呢?
高手给个意见阿^_^

论坛徽章:
0
2 [报告]
发表于 2007-02-02 11:13 |只看该作者
.asp后缀的网页只会当asp解析,是不会调用php进行解析的,既然是asp网站,为何不直接用asp做?

论坛徽章:
0
3 [报告]
发表于 2007-02-02 13:50 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2007-02-02 14:58 |只看该作者
做网页的试一小组,他们只做asp和windows的,我们又是另外一小组,不会asp只会linux和C,java,所以我们想用别的方法来实现的。
那楼上的你知道有什么方法马?

论坛徽章:
0
5 [报告]
发表于 2007-02-02 16:53 |只看该作者
ajax+php+mysql+linux+apche

论坛徽章:
0
6 [报告]
发表于 2007-02-03 11:37 |只看该作者
如果你要用asp做也行啊
asp+ajax+access(sql server)要想像web gtalk那样还得用C长连接

论坛徽章:
0
7 [报告]
发表于 2007-02-04 20:46 |只看该作者
.............

论坛徽章:
0
8 [报告]
发表于 2007-02-05 08:40 |只看该作者
php+ajax可以实现 网上也有源代码的

论坛徽章:
0
9 [报告]
发表于 2007-02-05 10:05 |只看该作者
不懂什么ajax,按概念理解,自己搞了个自己的AJAX。聊天PHP代码(文件名test.php)如下:

  1. <?php
  2. global $callback;

  3. if ($callback) {
  4.         global $getsource, $isframe, $name, $msg, $refresh, $getall;
  5.         if ($isframe) print "<script text=\"text/javascript\">parent.";
  6.         if ($getsource) {
  7.                 $src = highlight_file(__FILE__, TRUE);
  8.                 $src = ereg_replace ("\r|\n", "", $src);
  9.                 $src = addslashes($src);
  10.                 print "$callback('$src');";
  11.         }
  12.         else if (($name && $msg) || $refresh) {
  13.                 $chatlog = dirname(__FILE__) . "/chat.log";
  14.                 $date = date("Y-m-d h:i:s");
  15.                 $addr = $_SERVER["REMOTE_ADDR"];
  16.                 $msg_content = htmlspecialchars("$date [$addr][$name]:$msg");
  17.                 if ($refresh) {
  18.                         $chatlogmsg = file_get_contents($chatlog);
  19.                 }
  20.                 else {
  21.                         $chatlogmsg = file_get_contents($chatlog) . "\n$msg_content";
  22.                         file_put_contents($chatlog, $chatlogmsg);
  23.                 }
  24.                 if (!$getall) {
  25.                         $chatlogmsg = explode("\n", $chatlogmsg);
  26.                         $chatcount = count($chatlogmsg);
  27.                         $chatlogmsg = implode("\n", array_slice($chatlogmsg, $chatcount - 20 > 0 ? $chatcount - 20: 0, 20));
  28.                 }
  29.                 $chatlogmsg = ereg_replace("\n", "','", $chatlogmsg);
  30.                 print "$callback(['$chatlogmsg']);";
  31.         }
  32.         else
  33.                 print "$callback(1);";
  34.         if ($isframe) print "</script>";
  35. } else {?>
  36. <html><head>
  37. <title>网页部分刷新测试</title>
  38. <script text="text/javascript" src="ajax.js"></script>
  39. <script text="text/javascript">
  40. new scriptRefresh(1000, "test.php?test=ajaxchat&refresh=1", "PostMessageOK");

  41. function PostMessage() {
  42.         var url = "test.php?test=ajaxchat&name=" + document.getElementById("name").value + "&msg=" + document.getElementById("msg").value;
  43.         new scriptRequest(url, "PostMessageOK", false);
  44.         document.getElementById("post_status").innerHTML = "发送中,请稍后.....";
  45. }

  46. function PostMessageFrame() {
  47.         document.getElementById("post_status").innerHTML = "发送中,请稍后.....";
  48.         return true;
  49. }

  50. function PostMessageOK(isOK) {
  51.         if (isOK) {
  52.                 document.getElementById("post_status").innerHTML = "";
  53.                 if (isOK != 1) {
  54.                         var msg_content = "<table>";
  55.                         for (var i = 0; i < isOK.length; i++)
  56.                                 msg_content += "<tr><td>" + isOK[i] + "</td></tr>";
  57.                         msg_content += "</table>";
  58.                         document.getElementById("msg_content").innerHTML = msg_content;
  59.                 }
  60.         }
  61.         else
  62.                 document.getElementById("post_status").innerHTML = "发送失败!";
  63. }

  64. function PrintSource() {
  65.         new scriptRequest("test.php?test=ajaxchat&getsource=1", "PrintSourceOK", true);
  66.         document.getElementById("pageText").innerHTML = "读取中,请稍后.....";
  67. }

  68. function PrintSourceFrame() {
  69.         document.getElementById("pageText").innerHTML = "系统检查中,请稍后.....";
  70.         return true;
  71. }

  72. function PrintSourceOK(text) {
  73.         document.getElementById("pageText").innerHTML = text;
  74. }

  75. function CloseSourcePrint() {
  76.         document.getElementById("pageText").innerHTML = "";
  77. }
  78. </script>
  79. </head>
  80. <body>

  81. <iframe id="Checkframe" name="Checkframe" width="0" height="0"></iframe>
  82. <fieldset style="background-color: #eee"><legend>ajax分布式测试</lengend>
  83. <div id="msg_content"></div>
  84. </fieldset>
  85. <form action="test.php?test=ajaxchat" method="post" target="Checkframe">
  86. <input type="hidden" name="callback" value="PostMessageOK">
  87. 姓名:<input type="text" name="name" size=15 id="name" value="">
  88. 信息:<input type="text" name="msg" size=50 id="msg" value="">
  89. <input type="hidden" name="isframe" value=1>
  90. <input type="submit" onclick="return PostMessageFrame();" value="SEND--iframe">
  91. <input type="button" onclick="PostMessage(); return false;" value="SEND--AJAX">
  92. <span id="post_status"></span><br>
  93. </form>
  94. <br>
  95. <form action="test.php?test=ajaxchat" method="post" target="Checkframe">
  96. <input type="hidden" name="getsource" value=1>
  97. <input type="hidden" name="isframe" value=1>
  98. <input type="hidden" name="callback" value="PrintSourceOK">
  99. <input type="submit" onclick="return PrintSourceFrame()" value="查看本页源代码--iframe">
  100. <input type="button" onclick="PrintSource(); return false;" value="查看本页源代码--AJAX">
  101. <input type="button" onclick="CloseSourcePrint(); return false;" value="关闭源代码显示">
  102. </form>

  103. <div id="pageText" style="background-color: #eee" ></div>
  104. </body>
  105. <?php } ?>
复制代码


其中使用的ajax.js是自己定义的,代码如下:

  1. scriptRequest = function (url, callBack, isCached, onTimeout, timeout) {
  2.         if (typeof onTimeout != "undefined") this.onTimeout = onTimeout;
  3.         if (typeof scriptRequest.requestQueue == "undefined")  scriptRequest.requestQueue = new Array();
  4.         scriptRequest.requestQueue.push(this);
  5.         this.cleanRequest = function () {
  6.                 if (this.timeout) window.clearTimeout(this.timeout);
  7.                 var jsRequest = document.getElementById("jsRequest" + this.hash);
  8.                 if (jsRequest != null && jsRequest.tagName.toLowerCase() == "script") {         
  9.                         try{
  10.                                 document.getElementsByTagName("head").item(0).removeChild(jsRequest);
  11.                         }catch(err){}
  12.                 }
  13.         }
  14.         if (typeof scriptRequest.callback == "undefined")
  15.                 scriptRequest.callback = function (hash) {
  16.                         for (var i = 0; i < scriptRequest.requestQueue.length; i++) {
  17.                                 var srRequest = scriptRequest.requestQueue[i];
  18.                                 if (srRequest && srRequest.hash == hash) {
  19.                                         if (srRequest.onTimeout) {
  20.                                                 try {
  21.                                                         eval(srRequest.onTimeout);
  22.                                                 } catch(err) {}
  23.                                         }
  24.                                         srRequest.cleanRequest();
  25.                                         delete srRequest;
  26.                                         break;
  27.                                 }
  28.                         }
  29.                 }

  30.         this.hash = new Date().getTime() + scriptRequest.requestQueue.length;
  31.         this.timeout = window.setTimeout("scriptRequest.callback('" + this.hash + "')", timeout ? timeout : 10000);

  32.         if (callBack) url += "&callback=" + callBack;
  33.         if (!isCached) url += "&hash=" + this.hash;
  34.         var jsRequest = document.createElement('script');
  35.         jsRequest.setAttribute("text","text/javaScript");
  36.         jsRequest.setAttribute("id","jsRequest" + this.hash);
  37.         jsRequest.setAttribute("src", url);
  38.         document.getElementsByTagName("head").item(0).appendChild(jsRequest);
  39. }

  40. scriptRefresh = function (interval, url, callback, onTimeout, timeout) {
  41.         this.userURL = url;
  42.         this.userInterval = interval;
  43.         this.userCallback = callback;
  44.         this.userOnTimeout = onTimeout;
  45.         this.userTimeout = timeout;
  46.         this.counter = 0;
  47.        
  48.         if (typeof scriptRefresh.timeout == "undefined")
  49.                 scriptRefresh.timeout = 0;
  50.         if (typeof scriptRefresh.interval == "undefined") scriptRefresh.interval = 100;
  51.         if (typeof scriptRefresh.refreshQueue == "undefined")  scriptRefresh.refreshQueue = new Array();
  52.         if (typeof scriptRefresh.refresh == "undefined")
  53.                 scriptRefresh.refresh = function () {
  54.                         for (var i = 0; i < scriptRefresh.refreshQueue.length; i++)
  55.                         {
  56.                                 var aRefresh = scriptRefresh.refreshQueue[i];
  57.                                 if (aRefresh && ++aRefresh.counter == aRefresh.userInterval / scriptRefresh.interval) {
  58.                                         new scriptRequest(aRefresh.userURL, aRefresh.userCallback, false, aRefresh.userOnTimeout, aRefresh.userTimeout);
  59.                                         aRefresh.counter = 0;
  60.                                 }
  61.                         }
  62.                 }
  63.         scriptRefresh.refreshQueue.push(this);
  64.         if (typeof scriptRefresh.callback == "undefined") {
  65.                 scriptRefresh.callback = function() {
  66.                                 if (scriptRefresh.timeout) window.clearTimeout(scriptRefresh.timeout);
  67.                                 scriptRefresh.refresh();
  68.                                 scriptRefresh.timeout = window.setTimeout("scriptRefresh.callback()", scriptRefresh.interval);
  69.                         }
  70.                 scriptRefresh.callback();
  71.         }
  72. }

  73. scriptRefresh.prototype.setInterval = function (interval) {
  74.         if (scriptRefresh.interval != interval) {
  75.                 for (var i = 0; i < scriptRefresh.refreshQueue.length; i++)
  76.                         scriptRefresh.refreshQueue[i].counter = scriptRefresh.refreshQueue[i].counter * this.interval / interval;
  77.         }
  78.         scriptRefresh.interval = interval;
  79. }
  80. scriptRefresh.prototype.removeRefresh = function (srRefresh) {
  81.         for (var i = 0; i < scriptRefresh.refreshQueue.length; i++)
  82.                 if (scriptRefresh.refreshQueue[i] == srRefresh) {
  83.                         delete scriptRefresh.refreshQueue[i];
  84.                         break;
  85.                 }
  86. }
复制代码

[ 本帖最后由 ftInternet 于 2007-2-5 10:11 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2007-02-05 10:08 |只看该作者
上面这个代码,可以使用ajax(自定义)通过更新DIV或FRAME两种方式实现。大家有兴趣有时间可以看看。提点意见。
俺不是专做网页的。只是对AJAX概念了解了一下,尝试做了个代码。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP