问题在使用 stompjs 的 Angular 应用中。

huangapple go评论62阅读模式
英文:

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 &#39;@angular/core&#39;;
import * as Stomp from &#39;stompjs&#39;;
import * as SockJS from &#39;sockjs-client&#39;;
import $ from &#39;jquery&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.css&#39;]
})
export class AppComponent {
  private serverUrl = &#39;http://localhost:8080/socket&#39;
  private title = &#39;WebSockets chat&#39;;
  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(&quot;/chat&quot;, (message) =&gt; {
        if(message.body) {
          $(&quot;.chat&quot;).append(&quot;&lt;div class=&#39;message&#39;&gt;&quot;+message.body+&quot;&lt;/div&gt;&quot;)
          console.log(message.body);
        }
      });
    });
  }

  sendMessage(message){
    this.stompClient.send(&quot;/app/send/message&quot; , {}, message);
    $(&#39;#input&#39;).val(&#39;&#39;);
  }

}

This is the following error that I perform ng-serve:

ERROR in ./node_modules/stompjs/lib/stomp-node.js
Module not found: Error: Can&#39;t resolve &#39;net&#39; in &#39;C:\Users\drnic\Desktop\Messaging app\Front-end\bantachat2\node_modules\stompjs\lib&#39;

答案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.

huangapple
  • 本文由 发表于 2020年1月6日 22:37:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定