\n\n    \n        {{--對(duì)話窗口頭部--}}\n        
\n            \n                \"workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用\"heading}}\" width=\"80px\" height=\"40px\">\n            <\/div>\n            \n                {{$to->name}}\n            <\/div>\n        <\/div>\n        {{--\/\/對(duì)話窗口內(nèi)容--}}\n        \n            {{--對(duì)方的頭像與文字--}}\n            {{----}}\n                {{----}}\n                    {{--\"workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用\"heading}}\" width=\"50px\" height=\"50px\">--}}\n                {{--<\/div>--}}\n                {{--dsadsadsadsadsa<\/div>--}}\n            {{--<\/div>--}}\n            {{--我的頭像與文字--}}\n            {{----}}\n                {{----}}\n                    {{--\"workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用\"heading}}\" width=\"50px\" height=\"50px\">--}}\n                {{--<\/div>--}}\n                {{--dsadsadsadsadsa<\/div>--}}\n            {{--<\/div>--}}\n        <\/div>\n        {{--對(duì)話發(fā)送窗口--}}\n        \n            id}}\">\n            id}}\">\n            \n            \n            <\/textarea>\n            
\n                \n                發(fā)表<\/button>\n            <\/div>\n        <\/form>\n        房間號(hào){{$room}}\n    <\/div>\n<\/div>\n<\/body>\n<\/html>\n

亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 php框架 Workerman workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用

workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用

Nov 22, 2019 pm 02:18 PM
workerman

下面由workerman php教程欄目給大家介紹workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的方法,希望對(duì)需要的朋友有所幫助!

workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用

項(xiàng)目背景:

最近由于公司的業(yè)務(wù)需求,需要用到聊天功能。而且有比較多的個(gè)性化需求需要定制。之前使用別人的聊天組件是基于微擎的。如果要移植到普通的H5在邏輯修改還有定制上存在比較多的困難。為此只能克服困難,自己搭建一個(gè)吧

什么是Workerman?

Workerman是一款開源高性能異步PHP socket即時(shí)通訊框架。支持高并發(fā),超高穩(wěn)定性,被廣泛的用于手機(jī)app、移動(dòng)通訊,微信小程序,手游服務(wù)端、網(wǎng)絡(luò)游戲、PHP聊天室、硬件通訊、智能家居、車聯(lián)網(wǎng)、物聯(lián)網(wǎng)等領(lǐng)域的開發(fā)。?

支持TCP長連接,支持Websocket、HTTP等協(xié)議,支持自定義協(xié)議。擁有異步Mysql、異步Redis、異步Http、MQTT物聯(lián)網(wǎng)客戶端、異步消息隊(duì)列等眾多高性能組件。

開始實(shí)戰(zhàn)吧!

1.第一步我們先把workerman里需要用到的擴(kuò)展composer下來吧

"workerman/gateway-worker": "^3.0",
"workerman/gatewayclient": "^3.0",
"workerman/workerman": "^3.5",

2.第二步我們到官方網(wǎng)站把demo全部下載下來,然后放到我們項(xiàng)目中的目錄圖片中我就把整個(gè)項(xiàng)目都放在了HTTP/Controller/Workerman中。

workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用

3.第三步我們需要把把以下3個(gè)文件的引用部分修改為以下。

不然會(huì)報(bào)路徑錯(cuò)誤

start_businessworker,start_gateway,start_register
require_once __DIR__ . &#39;/../../../../../vendor/autoload.php&#39;;

4.修改完成后我們就可以在liunx直接運(yùn)行對(duì)應(yīng)的啟動(dòng)文件

php start.php start -d
如果你是在window下就雙擊start_for_win.bat運(yùn)行

5.運(yùn)行成功后,你就應(yīng)該可以看到以下的界面到此我們搭建基于workerman的通信環(huán)境就已經(jīng)完成。

接下來我們就可以根據(jù)自己的項(xiàng)目需求進(jìn)行開發(fā)。

在此向大家重點(diǎn)說明。

我們所有的聊天是邏輯都在目錄中的Events.php進(jìn)行修改。


下面我給大家貼一下我編寫的部分代碼。

Event.php

<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
/**
 * 用于檢測(cè)業(yè)務(wù)代碼死循環(huán)或者長時(shí)間阻塞等問題
 * 如果發(fā)現(xiàn)業(yè)務(wù)卡死,可以將下面declare打開(去掉//注釋),并執(zhí)行php start.php reload
 * 然后觀察一段時(shí)間workerman.log看是否有process_timeout異常
 */
//declare(ticks=1);
/**
 * 聊天主邏輯
 * 主要是處理 onMessage onClose
 */
use \GatewayWorker\Lib\Gateway;
class Events
{
    /**
     * 作者:何志偉
     * 當(dāng)客戶端連接上來的時(shí)候
     * 創(chuàng)建時(shí)間:2018/10/25
     * @param $client_id 此ID為gatewayworker 自動(dòng)生成ID
     */
    public static function onConnect($client_id)
    {
        Gateway::sendToClient($client_id, json_encode(array(
            &#39;type&#39;      => &#39;init&#39;,
            &#39;client_id&#39; => $client_id
        )));
    }
    /**
     * 有消息時(shí)
     * @param int $client_id
     * @param mixed $message
     */
    public static function onMessage($client_id, $message)
    {
        // debug
        echo "client:{$_SERVER[&#39;REMOTE_ADDR&#39;]}:{$_SERVER[&#39;REMOTE_PORT&#39;]} gateway:{$_SERVER[&#39;GATEWAY_ADDR&#39;]}:{$_SERVER[&#39;GATEWAY_PORT&#39;]}  client_id:$client_id session:".json_encode($_SESSION)." onMessage:".$message."\n";
        // 客戶端傳遞的是json數(shù)據(jù)
        $message_data = json_decode($message, true);
        if(!$message_data)
        {
            return ;
        }
        // 根據(jù)類型執(zhí)行不同的業(yè)務(wù)
        switch($message_data[&#39;type&#39;])
        {
            // 客戶端回應(yīng)服務(wù)端的心跳
            case &#39;pong&#39;:
                return;
            // 客戶端登錄 message格式: {type:login, name:xx, room_id:1} ,添加到客戶端,廣播給所有客戶端xx進(jìn)入聊天室
            case &#39;login&#39;:
                // 判斷是否有房間號(hào)
                if(!isset($message_data[&#39;room_id&#39;]))
                {
                    throw new \Exception("\$message_data[&#39;room_id&#39;] not set. client_ip:{$_SERVER[&#39;REMOTE_ADDR&#39;]} \$message:$message");
                }
                // 把房間號(hào)昵稱放到session中
                $room_id = $message_data[&#39;room_id&#39;];
                $client_name = htmlspecialchars($message_data[&#39;client_name&#39;]);
                $_SESSION[&#39;room_id&#39;] = $room_id;
                $_SESSION[&#39;client_name&#39;] = $client_name;
                // 獲取房間內(nèi)所有用戶列表
                $clients_list = Gateway::getClientSessionsByGroup($room_id);
                foreach($clients_list as $tmp_client_id=>$item)
                {
                    $clients_list[$tmp_client_id] = $item[&#39;client_name&#39;];
                }
//                $clients_list[$client_id] = $client_name;
                // 轉(zhuǎn)播給當(dāng)前房間的所有客戶端,xx進(jìn)入聊天室 message {type:login, client_id:xx, name:xx}
                $new_message = array(&#39;type&#39;=>$message_data[&#39;type&#39;], &#39;client_id&#39;=>$client_id, &#39;client_name&#39;=>htmlspecialchars($client_name), &#39;time&#39;=>date(&#39;Y-m-d H:i:s&#39;),&#39;to&#39;=>$message_data[&#39;to&#39;],&#39;room_id&#39;=>$message_data[&#39;room_id&#39;],
                    &#39;from&#39;=>$message_data[&#39;from&#39;],&#39;tag&#39;=>$message_data[&#39;tag&#39;]);
                Gateway::sendToGroup($room_id, json_encode($new_message));
                Gateway::joinGroup($client_id, $room_id);
                // 給當(dāng)前用戶發(fā)送用戶列表
                $new_message[&#39;client_list&#39;] = $clients_list;
                Gateway::sendToCurrentClient(json_encode($new_message));
                return;
            // 客戶端發(fā)言 message: {type:say, to_client_id:xx, content:xx}
            case &#39;say&#39;:
                // 非法請(qǐng)求
                if(!isset($_SESSION[&#39;room_id&#39;]))
                {
                    throw new \Exception("\$_SESSION[&#39;room_id&#39;] not set. client_ip:{$_SERVER[&#39;REMOTE_ADDR&#39;]}");
                }
                $room_id = $_SESSION[&#39;room_id&#39;];
                $client_name = $_SESSION[&#39;client_name&#39;];
                // 私聊
//                if($message_data[&#39;to_client_id&#39;] != &#39;all&#39;)
//                {
//                    $new_message = array(
//                        &#39;type&#39;=>&#39;say&#39;,
//                        &#39;from_client_id&#39;=>$client_id,
//                        &#39;from_client_name&#39; =>$client_name,
//                        &#39;to_client_id&#39;=>$message_data[&#39;to_client_id&#39;],
//                        &#39;content&#39;=>"<b>對(duì)你說: </b>".nl2br(htmlspecialchars($message_data[&#39;content&#39;])),
//                        &#39;time&#39;=>date(&#39;Y-m-d H:i:s&#39;),
//                    );
//                    Gateway::sendToClient($message_data[&#39;to_client_id&#39;], json_encode($new_message));
//                    $new_message[&#39;content&#39;] = "<b>你對(duì)".htmlspecialchars($message_data[&#39;to_client_name&#39;])."說: </b>".nl2br(htmlspecialchars($message_data[&#39;content&#39;]));
//                    return Gateway::sendToCurrentClient(json_encode($new_message));
//                }
                $new_message = array(
                    &#39;type&#39;=>&#39;say&#39;,
                    &#39;from_client_id&#39;=>$client_id,
                    &#39;from_client_name&#39; =>$client_name,
                    &#39;to_client_id&#39;=>&#39;all&#39;,
                    &#39;content&#39;=>nl2br(htmlspecialchars($message_data[&#39;content&#39;])),
                    &#39;time&#39;=>date(&#39;Y-m-d H:i:s&#39;),
                );
                return Gateway::sendToGroup($room_id ,json_encode($new_message));
        }
    }
    /**
     * 當(dāng)客戶端斷開連接時(shí)
     * @param integer $client_id 客戶端id
     */
    public static function onClose($client_id)
    {
        // debug
        echo "client:{$_SERVER[&#39;REMOTE_ADDR&#39;]}:{$_SERVER[&#39;REMOTE_PORT&#39;]} gateway:{$_SERVER[&#39;GATEWAY_ADDR&#39;]}:{$_SERVER[&#39;GATEWAY_PORT&#39;]}  client_id:$client_id onClose:&#39;&#39;\n";
        // 從房間的客戶端列表中刪除
        if(isset($_SESSION[&#39;room_id&#39;]))
        {
            $room_id = $_SESSION[&#39;room_id&#39;];
            $new_message = array(&#39;type&#39;=>&#39;logout&#39;, &#39;from_client_id&#39;=>$client_id, &#39;from_client_name&#39;=>$_SESSION[&#39;client_name&#39;], &#39;time&#39;=>date(&#39;Y-m-d H:i:s&#39;));
            Gateway::sendToGroup($room_id, json_encode($new_message));
        }
    }
}

客戶端頁面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>與{{$to->name}}的對(duì)話</title>
    <script type="text/javascript" src="{{asset(&#39;js&#39;)}}/swfobject.js"></script>
    <script type="text/javascript" src="{{asset(&#39;js&#39;)}}/web_socket.js"></script>
    <script type="text/javascript" src="{{asset(&#39;js&#39;)}}/jquery.min.js"></script>
    <link href="{{asset(&#39;css&#39;)}}/jquery-sinaEmotion-2.1.0.min.css" rel="stylesheet">
    <link href="{{asset(&#39;css&#39;)}}/bootstrap.min.css" rel="stylesheet">
    <link href="{{asset(&#39;css&#39;)}}/style.css" rel="stylesheet">
    <script type="text/javascript" src="{{asset(&#39;js&#39;)}}/jquery-sinaEmotion-2.1.0.min.js"></script>
    {{--<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>--}}
</head>
<style>
    #sinaEmotion {
        z-index: 999;
        width: 373px;
        padding: 10px;
        display: none;
        font-size: 12px;
        background: #fff;
        overflow: hidden;
        position: absolute;
        border: 1px solid #e8e8e8;
        top: 100px;
        left: 542.5px;
    }
</style>
<body onload="connect();" style="margin: auto; text-align: center;">
<div style="margin: auto;">
    <div style="border: 1px solid red; height: 40px; width: 500px; margin: auto;">
        {{--對(duì)話窗口頭部--}}
        <div>
            <div style="width: 80px; height: 40px; border: 1px solid blue; float: left">
                <img  src="/static/imghw/default1.png"  data-src="{{$to- alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" >heading}}"  class="lazy"   width="80px" height="40px">
            </div>
            <div style="width: 150px; height: 40px; border: 1px solid blue; float: left">
                {{$to->name}}
            </div>
        </div>
        {{--//對(duì)話窗口內(nèi)容--}}
        <div style="width: 500px; height: 400px; border: 1px solid green; margin-top: 40px; overflow-y: auto">
            {{--對(duì)方的頭像與文字--}}
            {{--<div style="min-height: 50px;margin-top: 10px;">--}}
                {{--<div style="width: 50px;height: 50px; border: 1px solid red;  margin-left:10px; float: left">--}}
                    {{--<img  src="/static/imghw/default1.png"  data-src="{{$to- alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" >heading}}"  class="lazy"   width="50px" height="50px">--}}
                {{--</div>--}}
                {{--<div style="border: 1px solid red; float: left;  min-height: 50px" >dsadsadsadsadsa</div>--}}
            {{--</div>--}}
            {{--我的頭像與文字--}}
            {{--<div style= "min-height:50px;margin-top: 10px;">--}}
                {{--<div style="width: 50px;height: 50px; border: 1px solid red;  margin-left:10px; float: right">--}}
                    {{--<img  src="/static/imghw/default1.png"  data-src="{{$from- alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" >heading}}"  class="lazy"   width="50px" height="50px">--}}
                {{--</div>--}}
                {{--<div style="border: 1px solid red; float: right;  min-height: 50px" >dsadsadsadsadsa</div>--}}
            {{--</div>--}}
        </div>
        {{--對(duì)話發(fā)送窗口--}}
        <form onsubmit="return onSubmit(); return false;" id="ajaxfrom">
            <input type="hidden" name="to" value="{{$to->id}}">
            <input type="hidden" name="from" value="{{$from->id}}">
            <input type="hidden" name="room_id" value="{{$room}}">
            <input type="hidden" name="tag" value="{{$tag}}">
            <textarea id="textarea" name="content" style="margin: 0px; width: 501px; height: 213px;"></textarea>
            <div>
                <input type="button" class="btn btn-default face pull-left" value="表情" />
                <button type="submit" class="btn btn-default">發(fā)表</button>
            </div>
        </form>
        房間號(hào){{$room}}
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    if (typeof console == "undefined") {    this.console = { log: function (msg) {  } };}
    // 如果瀏覽器不支持websocket,會(huì)使用這個(gè)flash自動(dòng)模擬websocket協(xié)議,此過程對(duì)開發(fā)者透明
    WEB_SOCKET_SWF_LOCATION = "/swf/WebSocketMain.swf";
    // 開啟flash的websocket debug
    WEB_SOCKET_DEBUG = true;
    var ws, name, client_list={};
    var to_client_id="";
    // 連接服務(wù)端初始化函數(shù)
    function connect() {
        // 創(chuàng)建websocket 屆時(shí)可以替換為對(duì)應(yīng)的服務(wù)器地址
        ws = new WebSocket("ws://"+document.domain+":7272");
        // 當(dāng)socket連接打開時(shí),輸入用戶名
        ws.onopen = onopen;
        // 當(dāng)有消息時(shí)根據(jù)消息類型顯示不同信息
        ws.onmessage = onmessage;
        //當(dāng)連接丟失時(shí),調(diào)用連接方法嘗試重新連接
        ws.onclose = function() {
            console.log("連接關(guān)閉,定時(shí)重連");
            connect();
        };
        //當(dāng)操作報(bào)錯(cuò)時(shí),返回異常錯(cuò)誤
        ws.onerror = function() {
            console.log("出現(xiàn)錯(cuò)誤");
        };
        //發(fā)送ajax獲取當(dāng)前房間的通話記錄
        $.post("/get_record", { "room":"{{$room}}" },
            function(msg){
                $.each(msg,function (v,k) {
                    console.log(k);
                    //判斷
                    if(k.tag!="{{$tag}}"){
                        $(".content").append(
                            &#39;<div style="min-height: 50px;margin-top: 10px;">&#39; +
                            &#39;<div style="width: 50px;height: 50px; border: 1px solid red;  margin-left:10px; float: left">&#39;+
                            &#39;<img  src="/static/imghw/default1.png"  data-src="{{$to- alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" >heading}}"  class="lazy"   width="50px" height="50px">&#39;+
                            &#39;</div>&#39;+
                            &#39;<div style="border: 1px solid red; float: left;  min-height: 50px" >&#39;+k.content+&#39;</div>&#39;+
                            &#39;<div>&#39;
                        ).parseEmotion();
                    }else{
                        $(".content").append(
                            &#39;<div style="min-height: 50px;margin-top: 10px;">&#39; +
                            &#39;<div style="width: 50px;height: 50px; border: 1px solid red;  margin-left:10px; float: right">&#39;+
                            &#39;<img  src="/static/imghw/default1.png"  data-src="{{$from- alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" >heading}}"  class="lazy"   width="50px" height="50px">&#39;+
                            &#39;</div>&#39;+
                            &#39;<div style="border: 1px solid red; float: right;  min-height: 50px" >&#39;+k.content+&#39;</div>&#39;+
                            &#39;<div>&#39;
                        ).parseEmotion();
                    }
                })
            });
    }
    // 連接建立時(shí)發(fā)送登錄信息
    function onopen()
    {
        var login_data=&#39;{"type":"login","client_name":"{{$from->name}}","room_id":"{{$room}}","to":"{{$to->id}}","from":"{{$from->id}}","tag":"{{$tag}}"}&#39;;
        ws.send(login_data);
        console.log(&#39;登錄成功&#39;)
    }
    // 服務(wù)端發(fā)來消息時(shí)
    function onmessage(e)
    {
        var data = JSON.parse(e.data);
        switch(data[&#39;type&#39;]){
            // 服務(wù)端ping客戶端心跳
            case &#39;ping&#39;:
                ws.send(&#39;{"type":"pong"}&#39;);
                break;
            // 登錄 更新用戶列表
            case &#39;login&#39;:
                //講需要的發(fā)送ID保存到本地to_client_id變量中
                for(var p in data[&#39;client_list&#39;]){
                    to_client_id=p;
                }
                console.log(to_client_id);
                break;
            // 發(fā)言
            case &#39;say&#39;:
                console.log(data);
                say(data[&#39;from_client_id&#39;], data[&#39;from_client_name&#39;], data[&#39;content&#39;], data[&#39;time&#39;]);
                break;
            // 用戶退出 更新用戶列表
            case &#39;logout&#39;:
                console.log(data);
                break;
            case &#39;init&#39;:
                //此處可以發(fā)送ajax用于綁定不同的用戶ID和client
                console.log(data);
                break;
        }
    }
    // 提交對(duì)話
    function onSubmit() {
        //先檢查當(dāng)前的對(duì)話是否超過20條記錄數(shù)
        var count=true;
        //發(fā)送ajax獲取當(dāng)前房間的通話記錄
        $.ajax({
            url: "/check_count",
            type: "post",
            async:false,
            // cache: false,
            // contentType: false,
            // processData: false,
            data:{
            &#39;room&#39;:"1",
            },
            success: function (msg) {
                if(msg>10){
                    alert(&#39;當(dāng)前的對(duì)話已經(jīng)超過次數(shù),請(qǐng)購買對(duì)應(yīng)服務(wù)&#39;)
                    count=false;
                }
            }
        });
        if(count){
            var neirong=$("#textarea").val().replace(/"/g, &#39;\\"&#39;).replace(/\n/g,&#39;\\n&#39;).replace(/\r/g, &#39;\\r&#39;);
            //ajax先把對(duì)應(yīng)的內(nèi)容發(fā)送到后臺(tái)錄入,回調(diào)成功后才把信息發(fā)送
            var fm=$("#ajaxfrom")[0];
            var formData = new FormData(fm);
            $.ajax({
                url: "/record",
                type: "post",
                cache: false,
                contentType: false,
                processData: false,
                data: formData,
                beforeSend:function(){
                },
                success: function (msg) {
                    if(msg.code=="0"){
                        ws.send(&#39;{"type":"say","to_client_id":"all","to_client_name":"{{$to->name}}","content":"&#39;+neirong+&#39;"}&#39;);
                        //清空文本框內(nèi)容
                        $("#textarea").val("");
                        //強(qiáng)制定位光標(biāo)
                        $("#textarea").focus();
                    }else{
                    }
                }
            });
        }
        return false;
    }
    // 發(fā)言
    function say(from_client_id, from_client_name, content, time){
        //判斷當(dāng)前的用戶名稱與發(fā)送消息的名稱是否一致
        if( "{{$from->name}}" == from_client_name){
            $(".content").append(
                &#39;<div style="min-height: 50px;margin-top: 10px;">&#39; +
                &#39;<div style="width: 50px;height: 50px; border: 1px solid red;  margin-left:10px; float: right">&#39;+
                &#39;<img  src="/static/imghw/default1.png"  data-src="{{$from- alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" >heading}}"  class="lazy"   width="50px" height="50px">&#39;+
                &#39;</div>&#39;+
                &#39;<div style="border: 1px solid red; float: right;  min-height: 50px" >&#39;+content+&#39;</div>&#39;+
                &#39;<div>&#39;
            ).parseEmotion();
        }else{
            $(".content").append(
                &#39;<div style="min-height: 50px;margin-top: 10px;">&#39; +
                &#39;<div style="width: 50px;height: 50px; border: 1px solid red;  margin-left:10px; float: left">&#39;+
                &#39;<img  src="/static/imghw/default1.png"  data-src="{{$to- alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" >heading}}"  class="lazy"   width="50px" height="50px">&#39;+
                &#39;</div>&#39;+
                &#39;<div style="border: 1px solid red; float: left;  min-height: 50px" >&#39;+content+&#39;</div>&#39;+
                &#39;<div>&#39;
            ).parseEmotion();
        }
        // $("#dialog").append(&#39;<div><img  src="/static/imghw/default1.png"  data-src="http://lorempixel.com/38/38/?&#39;+from_client_id+&#39;"  class="lazy"   / alt="workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用" > &#39;+from_client_name+&#39; <br> &#39;+time+&#39;<div   style="max-width:90%"></div><p class="triangle-isosceles top">&#39;+content+&#39;</p> </div>&#39;).parseEmotion();
    }
    $(function(){
        //全局用戶ID
        select_client_id = &#39;all&#39;;
        //如果發(fā)送的用戶有變化則對(duì)應(yīng)的用戶ID進(jìn)行替換
        $("#client_list").change(function(){
            select_client_id = $("#client_list option:selected").attr("value");
        });
        //表情選擇
        $(&#39;.face&#39;).click(function(event){
            $(this).sinaEmotion();
            event.stopPropagation();
        });
    });
    // document.write(&#39;<meta name="viewport" content="width=device-width,initial-scale=1">&#39;);
    $("textarea").on("keydown", function(e) {
        //按enter鍵自動(dòng)提交
        if(e.keyCode === 13 && !e.ctrlKey) {
            e.preventDefault();
            $(&#39;form&#39;).submit();
            return false;
        }
        // 按ctrl+enter組合鍵換行
        if(e.keyCode === 13 && e.ctrlKey) {
            $(this).val(function(i,val){
                return val + "\n";
            });
        }
    });
</script>

復(fù)制代碼這兩個(gè)代碼片段其實(shí)就是主要運(yùn)行的核心片段。其他框架的自帶參數(shù)需要各位自己去根據(jù)文檔去調(diào)試優(yōu)化。到此基于workerman的聊天用于功能demo已經(jīng)搭建完畢。

以上是workerman結(jié)合laravel開發(fā)線上聊天應(yīng)用的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
實(shí)作Workerman文件中的文件上傳與下載 實(shí)作Workerman文件中的文件上傳與下載 Nov 08, 2023 pm 06:02 PM

實(shí)現(xiàn)Workerman文件中的文件上傳與下載,需要具體程式碼範(fàn)例引言:Workerman是一款高效能的PHP非同步網(wǎng)路通訊框架,具備簡潔、高效、易用等特點(diǎn)。在實(shí)際開發(fā)中,文件上傳和下載是常見的功能需求,本文將介紹如何使用Workerman框架實(shí)現(xiàn)文件的上傳和下載,並給出具體的程式碼範(fàn)例。一、檔案上傳:檔案上傳是指將本機(jī)上的檔案?jìng)鬏斨了欧鞫说牟僮?。下面是使?/p>

swoole和workerman哪個(gè)好 swoole和workerman哪個(gè)好 Apr 09, 2024 pm 07:00 PM

Swoole 和 Workerman 都是高效能 PHP 伺服器框架。 Swoole 以其非同步處理、出色的效能和可擴(kuò)展性而聞名,適用於需要處理大量並發(fā)請(qǐng)求和高吞吐量的專案。 Workerman 提供了非同步和同步模式的靈活性,具有直覺的 API,更適合易用性和處理較低並發(fā)量的專案。

如何實(shí)作Workerman文件的基本使用方法 如何實(shí)作Workerman文件的基本使用方法 Nov 08, 2023 am 11:46 AM

如何實(shí)現(xiàn)Workerman文件的基本使用方法簡介:Workerman是一個(gè)高效能的PHP開發(fā)框架,它可以幫助開發(fā)者輕鬆建立高並發(fā)的網(wǎng)路應(yīng)用程式。本文將介紹Workerman的基本使用方法,包括安裝和設(shè)定、建立服務(wù)和監(jiān)聽連接埠、處理客戶端請(qǐng)求等。並給出相應(yīng)的程式碼範(fàn)例。一、安裝並設(shè)定Workerman在命令列中輸入以下命令來安裝Workerman:c

如何實(shí)作Workerman文件中的定時(shí)器功能 如何實(shí)作Workerman文件中的定時(shí)器功能 Nov 08, 2023 pm 05:06 PM

如何實(shí)現(xiàn)Workerman文件中的定時(shí)器功能Workerman是一款強(qiáng)大的PHP非同步網(wǎng)路通訊框架,它提供了豐富的功能,其中就包括定時(shí)器功能。使用定時(shí)器可以在指定的時(shí)間間隔內(nèi)執(zhí)行程式碼,非常適合定時(shí)任務(wù)、輪詢等應(yīng)用程式場(chǎng)景。接下來,我將詳細(xì)介紹如何在Workerman中實(shí)現(xiàn)定時(shí)器功能,並提供具體的程式碼範(fàn)例。第一步:安裝Workerman首先,我們需要安裝Worker

Workerman開發(fā):如何實(shí)現(xiàn)基於UDP協(xié)定的即時(shí)視訊通話 Workerman開發(fā):如何實(shí)現(xiàn)基於UDP協(xié)定的即時(shí)視訊通話 Nov 08, 2023 am 08:03 AM

Workerman開發(fā):基於UDP協(xié)議的即時(shí)視訊通話摘要:本文將介紹如何使用Workerman框架實(shí)現(xiàn)基於UDP協(xié)議的即時(shí)視訊通話功能。我們將深入了解UDP協(xié)議的特點(diǎn),並透過程式碼範(fàn)例展示如何建立一個(gè)簡單但完整的即時(shí)視訊通話應(yīng)用程式。引言:在網(wǎng)路通訊中,即時(shí)視訊通話是一項(xiàng)非常重要的功能。傳統(tǒng)的TCP協(xié)定在實(shí)現(xiàn)即時(shí)性較高的視訊通話時(shí),可能會(huì)有傳輸延遲等問題。而UDP

如何實(shí)現(xiàn)Workerman文件中的反向代理功能 如何實(shí)現(xiàn)Workerman文件中的反向代理功能 Nov 08, 2023 pm 03:46 PM

如何實(shí)現(xiàn)Workerman文件中的反向代理功能,需要具體程式碼範(fàn)例簡介:Workerman是一款高效能的PHP多進(jìn)程網(wǎng)路通訊框架,提供了豐富的功能和強(qiáng)大的效能,廣泛應(yīng)用於Web即時(shí)通訊、長連接服務(wù)等場(chǎng)景。其中,Workerman也支援反向代理功能,可實(shí)現(xiàn)伺服器對(duì)外提供服務(wù)時(shí)的負(fù)載平衡和靜態(tài)資源快取等功能。本篇文章將介紹如何使用Workerman實(shí)現(xiàn)反向代理功

如何使用Workerman建構(gòu)高可用性負(fù)載平衡系統(tǒng) 如何使用Workerman建構(gòu)高可用性負(fù)載平衡系統(tǒng) Nov 07, 2023 pm 01:16 PM

如何使用Workerman建立高可用性負(fù)載平衡系統(tǒng),需要具體程式碼範(fàn)例在現(xiàn)代技術(shù)領(lǐng)域中,隨著網(wǎng)路的快速發(fā)展,越來越多的網(wǎng)站和應(yīng)用程式需要處理大量的並發(fā)請(qǐng)求。為了實(shí)現(xiàn)高可用性和高效能,負(fù)載平衡系統(tǒng)成為了必不可少的組件之一。本文將介紹如何使用PHP開源框架Workerman建構(gòu)一個(gè)高可用性的負(fù)載平衡系統(tǒng),並提供具體的程式碼範(fàn)例。一、Workerman簡介Worke

實(shí)現(xiàn)Workerman文件中的文件傳輸功能 實(shí)現(xiàn)Workerman文件中的文件傳輸功能 Nov 08, 2023 pm 03:39 PM

Workerman是基於PHP開發(fā)的高效能非同步事件驅(qū)動(dòng)框架,它可以輕鬆實(shí)現(xiàn)TCP/UDP協(xié)定下的長連線開發(fā)。除此之外,Workerman也提供了實(shí)現(xiàn)檔案?jìng)鬏數(shù)墓δ埽梢杂渺洞髾n案?jìng)鬏?、資料備份等場(chǎng)景。本文將介紹如何在Workerman中實(shí)現(xiàn)檔案?jìng)鬏敼δ?,並提供具體的程式碼範(fàn)例。一、文件上傳功能實(shí)現(xiàn)文件上傳功能需要客戶端將要上傳的文件傳送給服務(wù)端,服務(wù)端驗(yàn)證

See all articles