PHP message push (including source code)

Source: Internet
Author: User

Overview

Today is just Saturday, yesterday, someone in the group asked how to intern PHP Instant Messaging. I also searched the information, I intend to implement the PHP message push in the-ajax long polling. I think the introduction of PHP Instant Messaging is better:
Http://www.cnblogs.com/hnrainll/archive/2013/05/07/3064874.html
can refer to, other I will not say, reference links, better than for demonstration, there is no operation of the database in the things.

My Code

Entry Code index.html

<! DOCTYPE html><html><head>    <title>Anti-Ajax push</title>    <style>        . Send{color:#555;  text-align: left; }        . Require{color:blue;  text-align: right; }        . Content_box{text-align: center;                     margin: px;  Border: 1px solid #ddd;  padding: px; }    </style>    <script src="Http://code.jquery.com/jquery-1.11.2.min.js"></script></head><body>    <div class="Content_box" id="Content_box_title"  Style="Border:none;" >message box</div>    <div class="Content_box" id="Content_box">    </div>    <div Style="width:450px;margin:0 auto;" >        <select id="username" style="font-size:20px;" >            <option value="1" selected= "selected">1</option>            <option value="2">2</option>        </Select>        <input type="text" style="font-size:20px;" Value="" id="Send_text">        <button id="Btn_send" style="font-size:20px;" >Send</button>        <button id="Btn_link" style="font-size:20px">Connection</button>    </div>    <div class="Error_tip" id="Error_tip" style ="color:red;" >    </div>    <script>$( function(){            //Send Message$(' #btn_send '). Click ( function(){                varSend_text = $ (' #send_text '). Val ();if(Send_text.length <=0){                    $(' #error_tip '). HTML (' cannot enter a null value '); }Else{Send (Send_text); }            });//press ENTER to send message$(' #send_text '). On (' KeyUp ', function(e){                if(E.keycode = = -){                    $(' #btn_send '). Trigger (' click '); }            });//Establish a communication link$(' #btn_link '). Click ( function(){Connect ();var_this = $ ( This); _this.attr (' disabled ',true); _this.html (' Connected ');        }); });//Establish communication connection function         function connect(){$(' #content_box_title '). HTML ($ (' #username '). Val () +' The message window '); $.ajax ({data:{' user ':$(' #username '). Val ()}, URL:' ajaxpush.php ', type:' Get ', timeout:0, DataType:' JSON ', Success: function(data){$(' #content_box '). Append (' <div class= ' require ' > '+data.msg+' </div> ');                Connect ();        }            }); }//Send Message function         function send(massege){            varUser =$ (' #username '). Val (); $.getjson (' write.php ',{' msg ': Massege,' user ': User}, function(data){                if(DATA.SF) {                    $(' #content_box '). Append (' <div class= ' send ' > '+massege+' </div> '); $(' #send_text '). Val ("'); }Else{                    $(' #error_tip '). HTML (' Input Save error! ');        }            }); }</script></body></html>

Ajax processing Input write.php

<?php/** * Created by TXM. * TIME:2015/4/18 13:13 * Function: */$filename= DirName (__file__).'/data.txt ';$isread _file= DirName (__file__).'/isread.txt ';$user= DirName (__file__).'/user.txt ';//write message, message unread, who sends the messageFile_put_contents ($filename,$_get[' msg ']); File_put_contents ($isread _file,' 0 '); File_put_contents ($user,$_get[' user ']);EchoJson_encode (Array(' SF '=true));

Long polling push ajaxpush.php

<?php/** * Created by TXM. * TIME:2015/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);//Is the message sent by the other party, set the message to read, exit the loop.     if($isread==' 0 '&&$get _user==$user) {file_put_contents ($isread _file,' 1 '); Break; } Sleep (1);}EchoJson_encode (Array(' msg '=$msg));
effect

Source Code

Http://pan.baidu.com/s/1pJ5elpp

PHP message push (including source code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.