js Code:
var socket = new WebSocket("ws://47.94.80.240:12345");
socket.onopen = function (evt) {
console.log("socket");
//
var mes = {
type: 1
}
socket.send(JSON.stringify(mes));
};
part of php code:
public function startServer()
{
// $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$this->master = socket_create_listen($this->port);
if (!$this->master) {
throw new \Exception("listen $this->port fail !");
}
$this->runLog("Server Started : " . date("Y-m-d H:i:s"));
$this->runLog("Listening on : 47.94.80.240 port " . $this->port);
$this->runLog("Master socket : " . $this->master . "\n");
self::$connectPool[] = $this->master;
while (true) {
$readFds = self::$connectPool;
//
@socket_select($readFds, $writeFds, $e = ull, $this->timeout);
foreach ($readFds as $socket) {
//
if ($this->master == $socket) {
$client = socket_accept($this->master); //
$this->handShake = false;
if ($client < 0) {
$this->log("clinet connect false!");
continue;
} else {
//
if (count(self::$connectPool) > self::$maxConnectNum)
continue;
//
$this->connect($client);
}
} else {
//,
$bytes = @socket_recv($socket, $buffer, 2048, 0);
//
if ($bytes == 0) {
$this->disConnect($socket);
} else {
//
if (!$this->handShake) {
$this->doHandShake($socket, $buffer);
} else {
//
$buffer = $this->decode($buffer);
$this->parseMessage($buffer, $socket);
}
}
}
}
}
}