我正在尝试将一些事件从节点客户端发送到Laravel回显服务器。
互联网上没有太多的文档。
我尝试使用socket.emit()
来做到这一点,但是没有事件到达echo服务器。
这是我的app.ts代码
import express = require('express');
import Echo from "laravel-echo";
import SocketIoClient from "laravel-echo";
// Create a new express application instance
const app: express.Application = express();
let io = require('socket.io-client');
let echo = new Echo({
broadcaster: 'socket.io',
host: 'http://3.16.169.253:6001',
client: io
});
echo.channel('public').listen('drumdata', (e:any) => {
console.log("DATA RECEIVED");
console.log(e);
})
sendData();
async function sendData(){
for(let i=0;i<10;i++){
await sleep(2000)
echo.connector.options.auth.headers['X-Socket-ID'] = echo.connector.socket.id
echo.connector.socket.emit('drumdata', "public",{"eventData":"myData"}) ;
}
}
function sleep(ms:number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Echo-Server日志显示应用程序已加入频道,但未触发任何事件。
[3:25:25 PM] - S6s3SKH1L61C9U6NAAAr joined channel: public
[3:25:59 PM] - S6s3SKH1L61C9U6NAAAr left channel: public (transport close)
[3:26:08 PM] - qZljcx9BzDZOU-s8AAAs joined channel: public
[3:26:14 PM] - qZljcx9BzDZOU-s8AAAs left channel: public (transport close)
[3:27:02 PM] - 9--wTFOpWlZVIfw4AAAt joined channel: public
有人对此有建议吗?
编辑:
在DEBUG模式下运行laravel-echo-server显示消息已接收,但没有事件触发该通道。 我什至还添加了“ X-Socket-ID”身份验证标头。
2019-07-09T10:09:10.381Z socket.io:socket got packet {"type":2,"nsp":"/","data":["drumdata","public",{"eventData":"pincia"}]}
2019-07-09T10:09:10.381Z socket.io:socket emitting event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:10.381Z socket.io:socket dispatching an event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:12.350Z engine:ws received "42["drumdata","public",{"eventData":"pincia"}]"
2019-07-09T10:09:12.350Z engine:socket packet
2019-07-09T10:09:12.350Z socket.io-parser decoded 2["drumdata","public",{"eventData":"pincia"}] as {"type":2,"nsp":"/","data":["drumdata","public",{"eventData":"pincia"}]}
2019-07-09T10:09:12.350Z socket.io:socket got packet {"type":2,"nsp":"/","data":["drumdata","public",{"eventData":"pincia"}]}
2019-07-09T10:09:12.350Z socket.io:socket emitting event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:12.350Z socket.io:socket dispatching an event ["drumdata","public",{"eventData":"pincia"}]
2019-07-09T10:09:14.350Z engine:ws received "42["drumdata","public",{"eventData":"pincia"}]"
2019-07-09T10:09:14.350Z engine:socket packet
谢谢, 费德里科
答案 0 :(得分:0)
好的,所以我开始看这一切如何工作,该文档并没有多大帮助,但是看起来echo服务器使用http api来发布消息。在这种情况下,他们似乎推荐axios。该解决方案应该可以工作,但是我无法弄清楚通道的路径应该是什么。一旦怀疑了这一点,就应该看到事件是从节点服务器传播的。目前只有404个。
const express = require('express');
const Echo = require('laravel-echo');
const io = require('socket.io-client');
const axios = require('axios');
// Create a new express application instance
const app = express();
const echo = new Echo({
broadcaster: 'socket.io',
host: 'http://127.0.0.1:6001',
client: io
});
echo.channel('public').listen('drumdata', (e) => {
console.log("DATA RECEIVED");
console.log(e);
})
async function sendData(){
for(let i=0;i<10;i++){
await sleep(2000);
console.log('echoing'); // echo.socketId() is already set on X-Socket-Id thanks to Axios
axios.post('http://127.0.0.1:6001/public/drumdata', {"eventData":"myData"})
.then((response) => {
console.log(response);
});
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
sendData();