workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼_第1頁
workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼_第2頁
workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼_第3頁
workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼_第4頁
workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼//獲取房間內(nèi)所有用戶列表

$clients_list=Gateway::getClientSessionsByGroup($room_id);

foreach($clients_listas$tmp_client_id=$item)

$clients_list[$tmp_client_id]=$item['client_name'];

//$clients_list[$client_id]=$client_name;

//轉(zhuǎn)播給當(dāng)前房間的所有客戶端,xx進(jìn)入聊天室message{type:login,client_id:xx,name:xx}

$new_message=array('type'=$message_data['type'],'client_id'=$client_id,'client_name'=htmlspecialchars($client_name),'time'=date('Y-m-dH:i:s'),'to'=$message_data['to'],'room_id'=$message_data['room_id'],

'from'=$message_data['from'],'tag'=$message_data['tag']);

Gateway::sendToGroup($room_id,json_encode($new_message));

Gateway::joinGroup($client_id,$room_id);

//給當(dāng)前用戶發(fā)送用戶列表

$new_message['client_list']=$clients_list;

Gateway::sendToCurrentClient(json_encode($new_message));

return;

//客戶端發(fā)言message:{type:say,to_client_id:xx,content:xx}

case'say':

//非法請求

if(!isset($_SESSION['room_id']))

thrownew\Exception("\$_SESSION['room_id']notset.client_ip:{$_SERVER['REMOTE_ADDR']}");

$room_id=$_SESSION['room_id'];

$client_name=$_SESSION['client_name'];

//私聊

//if($message_data['to_client_id']!='all')

//$new_message=array(

//'type'='say',

//'from_client_id'=$client_id,

//'from_client_name'=$client_name,

//'to_client_id'=$message_data['to_client_id'],

//'content'="b對你說:/b".nl2br(htmlspecialchars($message_data['content'])),

//'time'=date('Y-m-dH:i:s'),

//);

//Gateway::sendToClient($message_data['to_client_id'],json_encode($new_message));

//$new_message['content']="b你對".htmlspecialchars($message_data['to_client_name'])."說:/b".nl2br(htmlspecialchars($message_data['content']));

//returnGateway::sendToCurrentClient(json_encode($new_message));

$new_message=array(

'type'='say',

'from_client_id'=$client_id,

'from_client_name'=$client_name,

'to_client_id'='all',

'content'=nl2br(htmlspecialchars($message_data['content'])),

'time'=date('Y-m-dH:i:s'),

returnGateway::sendToGroup($room_id,json_encode($new_message));

*當(dāng)客戶端斷開連接時

*@paraminteger$client_id客戶端id

publicstaticfunctiononClose($client_id)

//debug

echo"client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']}gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']}client_id:$client_idonClose:''\n";

//從房間的客戶端列表中刪除

if(isset($_SESSION['room_id']))

$room_id=$_SESSION['room_id'];

$new_message=array('type'='logout','from_client_id'=$client_id,'from_client_name'=$_SESSION['client_name'],'time'=date('Y-m-dH:i:s'));

Gateway::sendToGroup($room_id,json_encode($new_message));

客戶端頁面

!DOCTYPEhtml

htmllang="en"

head

metacharset="UTF-8"

title與{{$to-name}}的對話/title

scripttype="text/javascript"src="{{asset('js')}}/swfobject.js"/script

scripttype="text/javascript"src="{{asset('js')}}/web_socket.js"/script

scripttype="text/javascript"src="{{asset('js')}}/jquery.min.js"/script

linkhref="{{asset('css')}}/jquery-sinaEmotion-2.1.0.min.css"rel="externalnofollow"rel="stylesheet"

linkhref="{{asset('css')}}/bootstrap.min.css"rel="externalnofollow"rel="stylesheet"

linkhref="{{asset('css')}}/style.css"rel="externalnofollow"rel="stylesheet"

scripttype="text/javascript"src="{{asset('js')}}/jquery-sinaEmotion-2.1.0.min.js"/script

{{--scriptsrc="/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:1pxsolid#e8e8e8;

top:100px;

left:542.5px;

/style

bodyonload="connect();"

div

div

{{--對話窗口頭部--}}

div

div

imgsrc="{{$to-heading}}"width="80px"height="40px"

/div

div

{{$to-name}}

/div

/div

{{--//對話窗口內(nèi)容--}}

div

{{--對方的頭像與文字--}}

{{--div--}}

{{--div--}}

{{--imgsrc="{{$to-heading}}"width="50px"height="50px"--}}

{{--/div--}}

{{--divdsadsadsadsadsa/div--}}

{{--/div--}}

{{--我的頭像與文字--}}

{{--div"min-height:50px;margin-top:10px;"--}}

{{--div--}}

{{--imgsrc="{{$from-heading}}"width="50px"height="50px"--}}

{{--/div--}}

{{--divdsadsadsadsadsa/div--}}

{{--/div--}}

/div

{{--對話發(fā)送窗口--}}

formonsubmit="returnonSubmit();returnfalse;"id="ajaxfrom"

inputtype="hidden"name="to"value="{{$to-id}}"

inputtype="hidden"name="from"value="{{$from-id}}"

inputtype="hidden"name="room_id"value="{{$room}}"

inputtype="hidden"name="tag"value="{{$tag}}"

textareaid="textarea"name="content"/textarea

div

inputtype="button"value="表情"/

buttontype="submit"發(fā)表/button

/div

/form

房間號{{$room}}

/div

/div

/body

/html

scripttype="text/javascript"

if(typeofconsole=="undefined"){this.console={log:function(msg){}};}

//如果瀏覽器不支持websocket,會使用這個flash自動模擬websocket協(xié)議,此過程對開發(fā)者透明

WEB_SOCKET_SWF_LOCATION="/swf/WebSocketMain.swf";

//開啟flash的websocketdebug

WEB_SOCKET_DEBUG=true;

varws,name,client_list={};

varto_client_id="";

//連接服務(wù)端初始化函數(shù)

functionconnect(){

//創(chuàng)建websocket屆時可以替換為對應(yīng)的服務(wù)器地址

ws=newWebSocket("ws://"+document.domain+":7272");

//當(dāng)socket連接打開時,輸入用戶名

ws.onopen=onopen;

//當(dāng)有消息時根據(jù)消息類型顯示不同信息

ws.onmessage=onmessage;

//當(dāng)連接丟失時,調(diào)用連接方法嘗試重新連接

ws.onclose=function(){

console.log("連接關(guān)閉,定時重連");

connect();

//當(dāng)操作報錯時,返回異常錯誤

ws.onerror=function(){

console.log("出現(xiàn)錯誤");

//發(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(

'div'+

'div'+

'imgsrc="{{$to-heading}}"width="50px"height="50px"'+

'/div'+

'div'+k.content+'/div'+

'div'

).parseEmotion();

}else{

$(".content").append(

'div'+

'div'+

'imgsrc="{{$from-heading}}"width="50px"height="50px"'+

'/div'+

'div'+k.content+'/div'+

'div'

).parseEmotion();

//連接建立時發(fā)送登錄信息

functiononopen()

varlogin_data='{"type":"login","client_name":"{{$from-name}}","room_id":"{{$room}}","to":"{{$to-id}}","from":"{{$from-id}}","tag":"{{$tag}}"}';

ws.send(login_data);

console.log('登錄成功')

//服務(wù)端發(fā)來消息時

functiononmessage(e)

vardata=JSON.parse(e.data);

switch(data['type']){

//服務(wù)端ping客戶端心跳

case'ping':

ws.send('{"type":"pong"}');

break;

//登錄更新用戶列表

case'login':

//講需要的發(fā)送ID保存到本地to_client_id變量中

for(v

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論