如题,hbuilder做的客户端有个聊天功能,能支持SignalR吗,我web网页测试没有问题,放到hbuilder里面不行呢。求解!
5***@qq.com
- 发布:2017-07-06 11:11
- 更新:2020-07-23 16:37
- 阅读:3755
似乎用不了,signalr需要jquery,uni用不了jquery,找了个不需要jq的版本能连接上和接收消息,但是发送消息有问题.可以尝试下.net core版的signalr,好像这个版本的可以不用jq
微软有个例子,它用原生JS写的,运行在Chrome中是正常工作的。
我在UNI中,改写了一下代码,在Chrome中调试,可以发消息,但是接收消息的方法没有触发。代码如下:
<template>
<view class="content">
<view>
<input type="text" v-model="user">
<input type="text" v-model="message">
<button @click="sendMsg">send</button>
</view>
<view>
<text v-for="message in messages" :key="message"></text>
</view>
</view>
</template>
<script>
import * as signalR from "@aspnet/signalr";
import { Toast } from "../../utils/index"
export default {
data() {
return {
connection: null,
messages: [],
user: '',
message: ''
}
},
onLoad() {
this.connection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:61625/chatHub")
.build();
},
onReady () {
this.connection.start({ transport: ['webSockets'] }).catch(err => {
Toast(err);
})
this.connection.on("messageReceived", (user, message) => {
console.log('messageReceived')
var msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
var encodedMsg = user + " says " + msg;
this.messages.push(encodedMsg)
});
},
methods: {
sendMsg () {
this.connection.invoke("SendMessage", this.user, this.message).catch(function (err) {
return console.error(err.toString());
});
}
}
}
</script>
真机运行报错,无法运行,错误如下:
10:17:11.983 17:12.318 11603 11624 E console : [ERROR] reportJSException >>>> exception function:createInstance, exception:Exception: Error: module "common/request" is not defined
10:17:12.013 17:12.318 11603 11624 E console : require@(global function):8:663258
10:17:12.033 17:12.318 11603 11624 E console : (global function):8:663006
10:17:12.063 17:12.318 11603 11624 E console : (global function):13104:30
10:17:12.093 17:12.318 11603 11624 E console : E:\Projects\kingsland\app\node_modules\@aspnet\signalr\dist\esm\NodeHttpClient.js@(global function):13170:34
10:17:12.113 17:12.318 11603 11624 E console : __webpack_require__@(global function):1299:35
10:17:12.153 17:12.318 11603 11624 E console : E:\Projects\kingsland\app\node_modules\@aspnet\signalr\dist\esm\DefaultHttpClient.js@(global function):10683:42
10:17:12.183 17:12.318 11603 11624 E console : __webpack_require__@(global function):1299:35
10:17:12.213 17:12.318 11603 11624 E console : E:\Projects\kingsland\app\node_modules\@aspnet\signalr\dist\esm\index.js@(global function):13913:45
10:17:12.243 17:12.318 11603 11624 E console : __webpack_require__@(global function):1299:35
求指教。
DCloud_HB_WDL
是uniapp 还是5+? 什么项目类型?
2019-01-11 09:49
2***@qq.com
uniapp 连signalr
2019-01-11 20:58
w***@163.com
回复 DCloud_HB_WDL: uniapp可以用SignalR吗?有兼容问题吗
2019-09-17 20:31
2***@qq.com
回复 DCloud_HB_WDL: app项目,h5可以但是app运行不起 exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Exception: Error: module "common/request" is not defined
2020-03-02 14:28