我正在为我的项目使用 websocket 技术。
有一个文件服务器.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>");
};
现在在服务器中创建哪个会话.php我正在其他文件上使用相同的session_id,但它没有给我会话数据
$session_id= $_REQUEST['session_id'];
if(!empty($session_id)){
session_id($session_id);
}
session_start();
echo "<pre>";
print_r($_SESSION);
exit;
但它给了我空白数组
那么如何获取WebSocket在HTTP文件中创建的会话
<?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...');
}