Php stream#u socket#u server赢了';不工作,但你创造工作
我正在尝试在php5+和win7x64环境中使用websocket。 首先,我使用stream_socket_server通过示例创建一个服务器套接字(另存为test_socket1.php) 并运行命令:php test_socket1.php,获得“Server created”输出。 之后,我在chrome/IE中运行client.html,它将进入stream_select,直到超时才返回。 因此,我使用另一个示例,即使用socket_create(另存为test_socket2.php) 并运行命令:php test_socket2.php,获得“服务器启动…”输出。然后我运行相同的client.html,工作正常。为什么这个能用? client.html如下所示Php stream#u socket#u server赢了';不工作,但你创造工作,php,websocket,Php,Websocket,我正在尝试在php5+和win7x64环境中使用websocket。 首先,我使用stream_socket_server通过示例创建一个服务器套接字(另存为test_socket1.php) 并运行命令:php test_socket1.php,获得“Server created”输出。 之后,我在chrome/IE中运行client.html,它将进入stream_select,直到超时才返回。 因此,我使用另一个示例,即使用socket_create(另存为test_socket2.php
var host = "ws://127.0.0.1:8000"; // SET THIS TO YOUR SERVER
try {
socket = new WebSocket(host);
log('WebSocket - status '+socket.readyState);
socket.onopen = function(msg) {
log("Welcome - status "+this.readyState);
};
socket.onmessage = function(msg) {
log("Received: "+msg.data);
};
socket.onclose = function(msg) {
log("Disconnected - status "+this.readyState);
};
}
catch(ex){
log(ex);
}
我还在test_socket1中添加了一些检查点,试图找出问题,但是运气不好,并且有两个未使用的结果
....
$this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Failed: socket_create()");
socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("Failed: socket_option()");
socket_bind($this->master, '127.0.0.1', '8000') or die("Failed: socket_bind()");
socket_listen($this->master,20) or die("Failed: socket_listen()");
$this->sockets['m'] = $this->master;
$this->stdout("Server started\nMaster socket: ".$this->master);
....
var host = "ws://127.0.0.1:8000"; // SET THIS TO YOUR SERVER
try {
socket = new WebSocket(host);
log('WebSocket - status '+socket.readyState);
socket.onopen = function(msg) {
log("Welcome - status "+this.readyState);
};
socket.onmessage = function(msg) {
log("Received: "+msg.data);
};
socket.onclose = function(msg) {
log("Disconnected - status "+this.readyState);
};
}
catch(ex){
log(ex);
}