.net 实现服务器推送消息signalr源码

源码在后面。
1、首先在neget 添加signalr.找到Microsoft.AspNet.SignalR。
在这里插入图片描述
2、添加Startup类
在这里插入图片描述

3、添加chatHub类 继承Hub.(方法需要和前台js一致)
在这里插入图片描述

3、html页面在这里插入图片描述
源码code

html

@{
ViewBag.Title = “Home Page”;
}

房间号: 用户名: 加入聊天室

消息: 发送

    ChatHub类
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hubs;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;

    namespace Demo
    {
    [HubName(“chatHub”)]
    public class ChatHub : Hub
    {
    public void AddToRoom(string groupId, string userName)
    {
    //将分组Id放到上下文中
    Groups.Add(Context.ConnectionId, groupId);
    //群发人员进入信息提示
    Clients.Group(groupId, new string[0]).addUserIn(groupId, userName);
    }
    public void Send(string groupId, string detail, string userName)
    {
    //Clients.All.addSomeMessage(detail);//群发给所有
    //发给某一个组
    Clients.Group(groupId, new string[0]).addSomeMessage(groupId, detail, userName);
    }
    }
    }
    ChatHub类
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hubs;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;

    namespace Demo
    {
    [HubName(“chatHub”)]
    public class ChatHub : Hub
    {
    public void AddToRoom(string groupId, string userName)
    {
    //将分组Id放到上下文中
    Groups.Add(Context.ConnectionId, groupId);
    //群发人员进入信息提示
    Clients.Group(groupId, new string[0]).addUserIn(groupId, userName);
    }
    public void Send(string groupId, string detail, string userName)
    {
    //Clients.All.addSomeMessage(detail);//群发给所有
    //发给某一个组
    Clients.Group(groupId, new string[0]).addSomeMessage(groupId, detail, userName);
    }
    }
    }

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值