阅读 102

SignalR:React + ASP.NET Core Api

一、 后台WebApi配置:

注:Vision为业务名称,以此为例,可随意修改

1. 安装包:Microsoft.AspNetCore.SignalR

2. 注入

Startup.cs ConfigureServices 添加以下代码:

services.AddSignalR();

Startup.cs Configure app.UseEndpoints 添加以下代码:

endpoints.MapHub("/api/visionHub");

3. 创建通信中心类,继承Hub

using Microsoft.AspNetCore.SignalR;

public class VisionHub : Hub
{
    //重写连接事件,初次建立连接时进入此方法,开展具体业务可使用,例如管理连接池。
    public override async Task OnConnectedAsync()
    {
        await Clients.Caller.SendAsync("connected", Context.ConnectionId);
    }
    //重写断开事件,同理。
    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await base.OnDisconnectedAsync(exception);
    }
    //服务端接收客户端发送方法
    public async Task SendMessage(string message)
    {
        //第一个参数为客户端接收服务端返回方法,名称需要服务端一致。
        await Clients.Caller.SendAsync("ReceiveMessage", Context.ConnectionId + ":  " + message);
    }
    public async Task SendAllMessage(string message)
    {
        //第一个参数为客户端接收服务端返回方法,名称需要服务端一致。
        await Clients.All.SendAsync("ReceiveMessage", Context.ConnectionId + ":  " + message);
    }
}

//Clients.All是全员广播,Clients.Caller是当前连接对象,也可以指定连接推送Clients.Client(id),id为在全局对象中获取:Context.ConnectionId

二、前台React配置

1. 安装包:npm install @microsoft/signalr

2. 连接配置

import React, { useState, useEffect, useRef } from ‘react‘;
import * as signalR from ‘@microsoft/signalr‘;

const Vision = () => {
    const connectionRef = useRef(null);
    useEffect(() => {
        let cc;
        const protocol = new signalR.JsonHubProtocol();
        const transport = signalR.HttpTransportType.WebSockets;

        const options = {
            transport,
            skipNegotiation: true,
        };
        cc = new signalR.HubConnectionBuilder()
            .withUrl("http://localhost:8090/api/visionHub", options)  //url需与后台注入一致。
            .withHubProtocol(protocol)
            .withAutomaticReconnect()
            .build();
        cc.serverTimeoutInMilliseconds = 60 * 1000 * 60 * 24;
        cc.on(‘connected‘, onConnected)
        cc.on(‘ReceiveMessage‘, onReceiveMessage)
        cc.onreconnected(() => {
            if (cc.connectionState != "Connected") {
                onRegister();
            }
        });
        cc.start().then(() => {
            connectionRef.current = cc;
        }).catch(err => {
            console.error(‘SignalR Connection Error: ‘, err);
        });
        return () => {
            if (cc) {
                cc.stop();
            }
        }
    }, [])
    const onConnected = (connectionId) => {
        document.getElementById("user").innerHTML = document.getElementById("user").innerHTML + connectionId;
    }
    const onReceiveMessage = (message) => {
        document.getElementById("receiveMsg").innerHTML = document.getElementById("receiveMsg").innerHTML + message + "
"; } const SendMessage = (method) => { connectionRef.current.invoke(method, document.getElementById("sendMsg").value).catch((err) => { console.error(‘Send‘, err); }); } return (
张三(ConnectionId):
服务器:
) } export default Vision

三、效果图

模拟张三上线,生成ConnectionId,单独与广播两种方式与服务器通信

更多强大功能请阅读官方文档:

原文:https://www.cnblogs.com/WinterSir/p/14595654.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐