如何使用asp.net核心中的signalR向组发送通知

时间:2018-02-07 06:41:45

标签: c# asp.net-core signalr

这里我们使用的是asp.net核心的singnalR alpha2版本。如何发送组(用户组)的通知。如果有任何样本可用于此处的示例链接。

在Hub中使用group写入方法。

public class SignalRCommonHub:Hub     {

//query fetching data from database
if (!$query)
            return false; //if there is no data in query return false
            // Starting the PHPExcel library
            $this->load->library('PHPExcel');//loading library
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

            $objPHPExcel->setActiveSheetIndex(0);

            // Field names in the first row
            $fields = $query->list_fields();
            $col = 0;
    //add data into cells
            foreach ($fields as $field) {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
                $col++;
            }

            // Fetching the table data
            $row = 2;
            foreach ($query->result() as $data) {
                $col = 0;
                foreach ($fields as $field) {
                    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
                    $col++;
                }
                $row++;
            }

            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'csv');
    //creating excel sheet
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="subscribers_sheet.csv"');
            header('Cache-Control: max-age=0');
            header('Cache-Control: max-age=1');
            header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
            header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
            header('Pragma: public'); // HTTP/1.0

            $objWriter->save('php://output');// inserting data into excel sheet

在称为hub方法的控制器中。

private readonly IHubContext isignalRhub;

    public void SendToGroup(int groupId, int userId)
    {
        Clients.Group(groupId.ToString()).InvokeAsync("refresh", groupId, userId);
    }
}

客户端在客户端调用方法时不触发。

基于群组(用户组)的用户在线状态更新使用signalR样本可用意味着在此处链接。建议如何实现用户在线状态。

谢谢,

1 个答案:

答案 0 :(得分:1)

从前端界面

,您需要设置集线器/连接,然后使用connectionId和组ID /名称调用添加/删除组。

例如:await _viewerHubContext.Groups.AddAsync(connectionId, groupName);

然后,当您广播呼叫时,您只需使用组ID /名称,signalR将发送给这些组。

例如:return Clients.Group(groupName).InvokeAsync("Send", "SendData", data);

相关问题