reactjs - 将 Strophe 与 React JS 集成
问题描述
我正在尝试使用 React JS 登录我的 XMPP 服务器。
我是 React 的新手,所以不知道我做错了什么,请在这里看看你是否可以帮助我。
import './App.css';
var BOSH_SERVICE = 'ws://chat.example.com:7070/ws';
var connection = null;
const Strophe = require("strophe.js").Strophe;
console.log("Strophe is " , Strophe);
Strophe.LogLevel = 0;
connection = new Strophe.Connection(BOSH_SERVICE, { 'keepalive': true });
connection.connect("rajan@example.com","jagruti", onConnect);
console.log("New Connection is " , connection);
function App() {
return (
<div className="App">
<p>Strophe React Example</p>
</div>
);
}
function onConnect(status) {
if (status == Strophe.Status.CONNECTING) {
console.log('Synergy is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
console.log('Synergy failed to connect.');
} else if (status == Strophe.Status.DISCONNECTING) {
console.log('Synergy is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
console.log('Synergy is disconnected.');
} else if (status == Strophe.Status.CONNECTED) {
console.log('Synergy is connected.');
// set presence
connection.addHandler(onMessage, null, 'message', null, null, null);
connection.send($pres().tree());
connection.addHandler(onPresence, null, "presence");
connection.sendIQ($iq({ type: "get" }).c("query", { xmlns: Strophe.NS.ROSTER }).tree(), onRoster);
}
}
但是在编译过程中,我不断收到此错误:
src/App.js
Line 39:23: '$pres' is not defined no-undef
Line 40:29: 'onPresence' is not defined no-undef
Line 41:25: '$iq' is not defined no-undef
解决方案
您还需要导入这些。
import { Strophe, $pres, $iq, onPresence } from 'strophe.js'
推荐阅读
- javascript - 如何重定向页面,等到完全加载后再查询
- iis - 将 osclass htaccess 文件转换为 web.config 以在 IIS 问题上运行 Php
- c++ - 为什么 std::enable_if 需要第二种模板类型?
- android - 反应原生 android/app/build.gradle 文件丢失
- r - 在 R 中解析和评估字符串表达式的列?
- reactjs - 当 ReactJS 应用程序中的增量计数超过一定数量时显示“警告”消息
- python - LSTM 模型准确率停留在一定水平并且不会改变
- c++ - 在库开发中使用派生方法
- c# - 是否可以以特定角度移动 2D 角色?
- javascript - 通过 API 在 Slider Revolution 插件中为每张幻灯片嵌套和检索变量