【问题标题】:Why does this error: "TypeError: _websocket_js__WEBPACK_IMPORTED_MODULE_0__.default.send is not a function" occur when the socket is imported?为什么在导入套接字时会出现此错误:“TypeError: _websocket_js__WEBPACK_IMPORTED_MODULE_0__.default.send is not a function”?
【发布时间】:2019-06-10 14:38:17
【问题描述】:

我是 react/redux 的新手,但我正在尝试使用它来构建一个前端,该前端将通过 RabbitMQ 与后端交互。我有一个 websockets.js 文件,我在其中建立了我的 websocket 和 STOMP 客户端:

import * as Stomp from 'stompjs';
var msgpack = require("msgpack-lite");

var onConn = () => {
  console.log('Successfully Connected');
  stompClient.subscribe('/queue/frontend-response', onMessage);
 //var params = { "params": "{}" };
 //var obj = JSON.stringify(msgpack.encode(params));
 //stompClient.send(
 //  '/exchange/product/product.request.find', 
 //  {'reply-to': 'frontend',  'correlation-id': 999}, 
 //  obj);
};

var onErr = (e) => {
  console.log('***************** Error:  ' + e);
};

function onMessage(msg) {
  console.log(msg);
}

var wss = new WebSocket("MY AMQP INSTANBCE");
var stompClient = Stomp.over(wss);
stompClient.connect(uname, pword, onConn, onErr, uname);

export default { stompClient }; 

如果我取消注释onConn 中的代码,则会发送数据并正确接收响应。但是,我希望任何需要与后端通信的组件都可以导入这个套接字。我的actions/index.js 文件中有这个:

// src/js/actions/index.js
import stompClient from './websocket.js';
import { SET_PRODUCTS } from "../constants/action-types";

var msgpack = require("msgpack-lite");

export function setProducts(payload) {
  var params = { "params": "{}" };
  var obj = JSON.stringify(msgpack.encode(params));
  stompClient.send(
    '/exchange/product/product.request.find', 
    {'reply-to': 'frontend',  'correlation-id': 999}, 
    obj);
  return { type: SET_PRODUCTS, payload }
};

目前,当在 UI 中单击按钮时,会执行此代码。请注意setProducts 中的代码与websockets.js 中的代码相同。但是在这里我没有得到任何数据,我得到了标题中提到的错误。

有人能解释一下这里发生了什么,并建议我可以在我的websockets.js 文件之外进行这项工作吗?

【问题讨论】:

    标签: redux websocket stomp


    【解决方案1】:

    叹息...我将export default { stompClient }; 更改为export default stompClient;,它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2016-05-20
      • 2018-08-05
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 2021-06-18
      相关资源
      最近更新 更多