英文:
problem in using stompjs using angular application
问题
以下是您的代码部分的中文翻译:
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 聊天';
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('');
}
}
请注意,我仅翻译了代码部分,没有包括错误消息。如果您需要有关错误消息的帮助,请告诉我。
英文:
This is my code for app component ts where I want to communicate to a websocket (for which I installed sockjs-client
and stompjs
). I don't know how to solve the error.
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('');
}
}
This is the following error that I perform 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'
答案1
得分: 2
使用以下命令安装依赖包 'net':
npm i net -S
英文:
Install dependency package 'net' using command
'npm i net -S'
答案2
得分: 0
Node stomp 是一个服务器端的 Node.js 包。这意味着它期望整个 node.js
堆栈已安装并在使用中,而这在 Angular 应用程序中并非如此。
这就是为什么问题出在库找不到 net
依赖项。所以基本上你安装了错误的东西。
https://github.com/jmesnil/stomp-websocket/blob/master/lib/stomp.js
这是浏览器文件。Node 版本只能在 Node.js 环境中运行。它使用一些 Node 模块来调用 require。
英文:
Node stomp is a serverside node.js package. This means that is expects the entire node.js
stack to be installed and used, which isn't the case in an angular application.
That's why the problem is that the library can't find the net
dependency. So basically you have installed the wrong thing.
Check a very similar issue here
> https://github.com/jmesnil/stomp-websocket/blob/master/lib/stomp.js
>
>Is the browser file. The node one will only work in a NodeJS Environment. It calls require using some Node modules.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论