这是我的应用程序组件ts的代码,我想在其中与websocket (我为其安装了sockjs-client和stompjs)进行通信。我不知道如何解决这个错误。
import { Component } from '@angular/core';
import * as Stomp from 'stompjs';
import * as SockJS from 'sockjs-client';
import $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private serverUrl = 'http://localhost:8080/socket'
private title = 'WebSockets chat';
private stompClient;
constructor(){
this.initializeWebSocketConnection();
}
initializeWebSocketConnection(){
let ws = new SockJS(this.serverUrl);
this.stompClient = Stomp.over(ws);
let that = this;
this.stompClient.connect({}, function(frame) {
that.stompClient.subscribe("/chat", (message) => {
if(message.body) {
$(".chat").append("<div class='message'>"+message.body+"</div>")
console.log(message.body);
}
});
});
}
sendMessage(message){
this.stompClient.send("/app/send/message" , {}, message);
$('#input').val('');
}
}这是我执行ng-serve时出现的以下错误:
ERROR in ./node_modules/stompjs/lib/stomp-node.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\drnic\Desktop\Messaging app\Front-end\bantachat2\node_modules\stompjs\lib'发布于 2020-04-21 20:19:58
使用命令安装依赖包'net‘
‘'npm net -S’
发布于 2020-01-06 23:07:28
Node stomp是一个服务器端node.js包。这意味着需要安装和使用整个node.js堆栈,而在angular应用程序中则不是这样。
这就是为什么这个库找不到net依赖的原因。所以基本上你安装了错误的东西。
Check a very similar issue here
https://github.com/jmesnil/stomp-websocket/blob/master/lib/stomp.js
是浏览器文件。节点1只能在NodeJS环境中工作。它的调用需要使用一些节点模块。
https://stackoverflow.com/questions/59613958
复制相似问题