我正在为我的项目使用websocket技术。
有一个文件server.php是守护进程文件。
和客户端代码如下:
var wsUri = "ws://localhost:8590/demo/server.php";
var websocket = new WebSocket(wsUri);
$(document).ready(function () {
startTimer();
websocket.onopen = function (ev) { // connection is open
$('#message_box').append("<div class=\"system_msg\">Connected!</div>"); //notify user
}
//#### Message received from server?
websocket.onmessage = function (ev) {
var msg = JSON.parse(ev.data); //PHP sends Json data
var type = msg.type; //message type
var umsg = msg.message; //message text
var uname = msg.name; //user name
if (msg.action) {
startTimer();
}
if (!msg.action) {
if (type == 'usermsg')
{
$('#message_box').append("<span> Name :" + uname +"</span>");
if (umsg.indexOf("null") >= 0) {
} else {
//window.alert(1);
}
}
if (type == 'system')
{
$('#message_box').append("<span> system :" + umsg + "</span>");
}
}
};
websocket.onerror = function (ev) {
$('#message_box').append("<div class=\"system_error\">Error Occurred - " + ev.data + "</div>");
};
websocket.onclose = function (ev) {
$('#message_box').append("<div class=\"system_msg\">Connection Closed- " + ev.data + "</div>");
};
现在在server.php中创建了哪个会话我在其他文件上使用相同的session_id,但它没有给我会话数据
$session_id= $_REQUEST['session_id'];
if(!empty($session_id)){
session_id($session_id);
}
session_start();
echo "<pre>";
print_r($_SESSION);
exit;
但它给我空白数组
所以如何在http文件
中获取websocket创建的会话答案 0 :(得分:0)
<?php
@session_start();
if(empty($_SESSION['session_id']) && !empty($_REQUEST['session_id'])) {
$_SESSION['session_id'] = $_REQUEST['session_id'];
}else {
print ('Error : can not initialize your session...');
}