0
我目前正在做的PHP的WebSocket送交
$room_array = array();
$room_array[0] = 'room-list';
$room_array['info'] = array('room_name' => $room['room_name'], 'owner' => $username['username'], 'room_description' => $room['room_desc']);
socket_send($this->current_socket, $this->encode(json_encode($room_array)), strlen($this->encode(json_encode($room_array))), 0);
以下內容,這是我的編碼功能
private function encode($text)
{
$b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($text);
if($length <= 125)
$header = pack('CC', $b1, $length);
elseif($length > 125 && $length < 65536)
$header = pack('CCS', $b1, 126, $length);
elseif($length >= 65536)
$header = pack('CCN', $b1, 127, $length);
return $header.$text;
}
而且我的客戶在我處理像這樣
message = JSON.parse(event.data);
console.log(message)
switch(message[0])
{
case 'error':
alert(message[1]);
break;
case 'login-success':
$('#user_information').html('<button class="user-name-button">Welcome <b>' + message.name + '</b></button><button class="user-coins-button"><b>' + message.coins + '</b> Coins</button>');
socket.send(JSON.stringify({'room_list': message.name}));
break;
case 'room-list':
alert(1)
break;
}
因此,使用此代碼alert(1)正在執行,但問題是如果我嘗試以下(添加更多元素到數組中)套接字不會工作。該消息將被髮送,但不會到達客戶端。不知道爲什麼
$room_array = array();
$room_array[0] = 'room-list';
$room_array['test'] = 'hello';
$room_array['info'] = array('room_name' => $room['room_name'], 'owner' => $username['username'], 'room_description' => $room['room_desc']);
socket_send($this->current_socket, $this->encode(json_encode($room_array)), strlen($this->encode(json_encode($room_array))), 0);
你的答案出現在低質量評價隊列中。請擴展您的答案。你可以編輯它。 – 2015-02-05 16:54:31