安装@microsoft/signalr
yarn add @microsoft/signalr
APP.vue
<script>
import * as signalR from '@microsoft/signalr'
const hubUrl = 'http://192.168.1.209:5001/ServerHub'
const connection = new signalR.HubConnectionBuilder()
.withAutomaticReconnect()
.withUrl(hubUrl, {
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets
})
.configureLogging(signalR.LogLevel.Information)
.build()
connection.start().catch(err => {
console.log(err)
})
connection.onreconnected(connectionId => {
console.log(connectionId)
})
export default {
created () {
if (window.localStorage.getItem('user_info')) {
this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(window.localStorage.getItem('user_info'))))
}
},
mounted () {
const _this = this
connection.on('SendMessageResponse', function (data) {
_this.remsg = _this.remsg + '<br>' + '接收数据:' + data
console.log('接受的数据:', data)
})
connection.on('ConnectResponse', function (data) {
_this.remsg = _this.remsg + '<br>' + '连接:' + data
})
},
methods: {
handle () {
if (this.msg.trim() === '') {
alert('不能发送空白消息')
return
}
connection.invoke('SendMessage', this.user, this.msg)
this.msg = ''
}
}
}
</script>
完善中。。。