所以我试图根据socket.io服务器自动更新charts.js,但每次我加载它时,客户端都没有出现。服务器端也没有说客户端已连接,它只是说正在侦听。我也没有收到任何错误。
服务器.js
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.get('/', (req, res)=> {
res.send('<h1>Hello World</h1>');
});
http.listen(3000, ()=>{
console.log('listning on port 3000');
});
io.on('connection', (socket) =>{
console.log('Client connected');
dataUpdate(socket);
});
function dataUpdate(socket) {
io.socket.emit('dataupdate', Array.from({length: 8}, ()=> Math.floor(Math.random() * 40)));
setTimeout(() =>{
dataUpdate(socket);
}, 2000)
}
套接字服务.ts
import { Injectable } from '@angular/core';
import {io} from 'socket.io-client/build/index';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SocketService {
socket: any;
constructor() {
this.socket = io('http://localhost:3000');
}
listen(Eventname: string) {
return new Observable((subscriber) => {
this.socket.on(Eventname, (data: any) => {
subscriber.next(data);
})
})
}
}
仅供参考,我正在关注这个 YouTube 教程
(抱歉英语不好,这不是我的第一语言。)