我目前正在PHP上执行以下操作
$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);
答案 0 :(得分:0)
使用单个对象。
$http->execute($room_array);
$room_array->parse_request(this->returnFive());
$http->send_request();
public returnFive(){
return 5;
}