我正在尝试使用Websocket将数据从node.js发送到Angular 7。 这就是我尝试过的:
'''NodeJs
app.use(cors({
origin:['ws://localhost:3535','ws://127.0.0.1:3535'],
credentials:true
}));
const wss = new WebSocket.Server({ port: 3535 })
wss.on('connection', ws => {
ws.on('message', message => {
console.log(`Received message => ${message}`)
})
ws.send('Hello! Message From Server!!')
})
'''
'''角度
import * as socketIo from 'socket.io-client';
var socket = socketIo('http://localhost:3535');
....
export class MyAppComponent implements OnInit {
constructor() { }
ngOnInit() {
socket.onopen = () => {
socket.send('Message From Client')
}
socket.onerror = (error) => {
console.log(`WebSocket error: ${error}`)
}
socket.onmessage = (e) => {
console.log(e.data)
}
}
'''
当我尝试运行后端时,尽管安装并导入它,但仍然出现cors访问问题,并且显示:
'''
Error -----------------------------------------------------------------
Message:
plugin.install is not a function
Stack:
TypeError: plugin.install is not a function
at plugins.forEach
----------------------------------------------------------------------
'''