要实时制作laravel应用程序,我想使用socket-io。
客户端是由离子框架编写的移动应用程序,在192.168.1.7:8100
运行。
配置并运行我安装的[laravel-echo-server][1]
插件io服务器。
这是运行socket io服务器的larvel echo服务器配置文件:
{
"authHost": "http://api.pars-app.dev",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "1924bf1d59b3759d",
"key": "keykeykey"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "3000",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": ""
}
首先,我使用此命令运行redis服务器:
redis-server --port 3001
然后使用此命令运行soketio服务器:
laravel-echo-server start
结果是这样的:
L A R A V E L E C H O S E R V E R
version 1.2.7
⚠ Starting server in DEV mode...
✔ Running at localhost on port 3000
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
这是Echo laravel js:
window.echo = new Echo({
broadcaster: 'socket.io',
host: 'http://192.168.1.7:3000',
auth: {
headers: {
Authorization: 'bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjUsImlzcyI6Imh0dHA6XC9cL2FwaS5wYXJzLWFwcC5kZXZcL3YxXC9hdXRoXC9zaWduSW4iLCJpYXQiOjE0OTI2NzU3OTgsImV4cCI6MTQ5NTI2Nzc5OCwibmJmIjoxNDkyNjc1Nzk4LCJqdGkiOiI5OTE3YTA4OWY0MDI4NjMwOTU5NWZmYjZmODQ2MTk5ZSJ9.oOFRmxQoiLS8BJksPkf6CQSVOcDR7rPApS71S6nZO5c'
}
}
});
但是当我在 Chrome 浏览器中运行主应用程序时,控制台出现以下错误:
XMLHttpRequest cannot load http://192.168.1.7:3000/socket.io/?EIO=3&transport=polling&t=LkQ5Rnb. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://192.168.1.7:8100' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我尝试了很多解决方案来解决这个问题,例如This Answer,但问题仍在继续。
答案 0 :(得分:1)
您是否尝试使用虚拟主机替换IP地址?
并像这样配置
ServerName your-domain.dev
DocumentRoot /var/www/html/your_project/public
<Directory "/var/www/html/your_project/public">
AllowOverride all
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT, PATCH"
Header set Access-Control-Max-Age "1000"
Header set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept"
</Directory>
运行SocketIO会收听并呼叫您的域名。
希望这有帮助。我已经在我的项目中使用了SocketIO。
我使用PM2运行SocketIO。