按照github上提示做的,網(wǎng)址:https://github.com/walkor/phpsocket.io/tree/master/docs/zh
截圖如下
但是打開火狐一直提示“已攔截跨源請求:同源策略禁止讀取位于 http://39.106.121.125:3120/socket.io/?EIO=3&transport=polling&t=MUbA4T7 的遠程資源”
截圖如下
?
?
客戶端訪問鏈接是:http://39.106.121.125/PHPCode/tools/web_socket/index.html
這個鏈接的源碼是
<html>
<head>
<script src='https://cdn.bootcss.com/socket.io/2.0.3/socket.io.js'></script>
<script>
// 如果服務端不在本機,請把127.0.0.1改成服務端ip
console.log('成立');
var socket = io('http://39.106.121.125:3120');
// 當連接服務端成功時觸發(fā)connect默認事件
socket.on('connect', function(){
console.log('connect success');
});
</script>
</head>
<body>
</body>
</html>
服務端php源碼
<?php
require_once '../web-msg-sender/vendor/autoload.php';
use Workerman\Worker;
use PHPSocketIO\SocketIO;
$io = new SocketIO(3120);
// 當有客戶端連接時
$io->on('connection', function($socket)use($io){
// 定義chat message事件回調(diào)函數(shù)
$socket->on('chat message', function($msg)use($io){
// 觸發(fā)所有客戶端定義的chat message from server事件
$io->emit('chat message from server', $msg);
});
});
Worker::runAll();
服務器啟動該php程序提示如下
?
請問大神該如何解決這個跨域問題?