shish 6 tahun lalu
induk
melakukan
384eb1b167

+ 12 - 4
app/business/controllers/ChatController.php

@@ -74,17 +74,25 @@ class ChatController extends BaseController
 				$arr['price'] = isset($info['price']) ? $info['price'] : 300;
 				break;
 		}
-
+		
 		$data[] = $arr;
-
+		
 		//每隔三分钟显示时间
 		$minute = (int)date("i");
 		if ($minute % 3 == 0) {
 			$time = date("i:s");
 			$data[] = ['type' => 'time', 'time' => $time];
 		}
-		Gateway::sendToUid($userId, json_encode($data));
+
+		$online = Gateway::getClientIdByUid($userId);
+		if ($online) {
+			//直接发送
+			Gateway::sendToUid($userId, json_encode($data));
+		} else {
+			//保存
+		}
+
 		util::success($data);
 	}
-
+	
 }

+ 27 - 13
app/business/views/chat/index.php

@@ -1,19 +1,33 @@
+<butto onclick="openMsg()">打开聊天</butto>
+<butto onclick="open()">打开</butto>
 <butto onclick="send()">发消息</butto>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <script>
-    ws = new WebSocket("ws://127.0.0.1:8282");
-    //服务端主动推送消息时会触发这里
-    ws.onmessage = function (e) {
-        var data = eval("(" + e.data + ")");
-        var type = data.type || '';
-        switch (type) {
-            case 'init':
-                //当前发起的聊天会员与当前登陆用户绑定关系
-                $.post('http://api.b.hhb.com/chat/bind', {clientId: data.client_id}, function (data) {
-                }, 'json');
-            default :
-        }
-    };
+    var clientId
+    function openMsg() {
+        ws = new WebSocket("ws://127.0.0.1:8282");
+        //服务端主动推送消息时会触发这里
+        ws.onmessage = function (e) {
+            var data = eval("(" + e.data + ")");
+            var type = data.type || '';
+            switch (type) {
+                case 'init':
+                    clientId = data.client_id;
+                    $.post('http://api.b.hhb.com/chat/bind', {clientId: data.client_id}, function (data) {
+
+                    }, 'json');
+                    console.log(clientId);
+                default :
+            }
+        };
+    }
+
+    function open() {
+        //当前发起的聊天会员与当前登陆用户绑定关系
+        $.post('http://api.b.hhb.com/chat/bind', {clientId: clientId}, function (data) {
+        
+        }, 'json');
+    }
 
     function send() {
         var type = 'text';

+ 7 - 0
app/client/controllers/ChatController.php

@@ -40,6 +40,13 @@ class ChatController extends BaseController
 			'type' => 'msg',
 			'msg' => $msg,
 		));
+		$online = Gateway::getClientIdByUid($id);
+		if ($online) {
+			//直接发送
+			Gateway::sendToUid($id, json_encode($data));
+		} else {
+			//保存
+		}
 		Gateway::sendToUid($id, $m);
 	}