5ug-signlar 中文文档教程

发布于 3年前 浏览 27 项目主页 更新于 3年前

适用于 ASP.NET Core 和 Azure SignalR 服务的 SignalR 的 JavaScript 和 TypeScript 客户端(与 DCloud/uni-app 平台兼容)。

Installation

npm install signalr-uni

Usage

有关最新版本的文档,请参阅 docs.microsoft.com 上的 SignalR 文档API 参考文档也可在 docs.microsoft.com 上找到.

有关将此客户端与 Azure SignalR 服务和 Azure Functions 结合使用的文档,请参阅 SignalR 服务无服务器开发人员指南

UniApp

要在 uni 应用程序中使用客户端,请将软件包安装到 node_modules 文件夹并使用 import { /*MODULE NAME*/ } from 'signalr-uni' 加载模块。

Browser

要在浏览器中使用客户端,请使用

WebWorker

要在 webworker 中使用客户端,请将 *.js 文件从 dist/webworker 文件夹复制到您的脚本文件夹中,包括使用 importScripts 功能。 请注意,webworker SignalR 集线器连接仅支持到 SignalR 集线器的绝对路径。

Node.js

要在 NodeJS 应用程序中使用客户端,请将包安装到 node_modules 文件夹并使用 require('@microsoft/signalr') 加载模块。 require('@microsoft/signalr') 返回的对象与全局 signalR 对象(在浏览器中使用时)具有相同的成员。

Example (UniApp)

import { HubConnectionBuilder, LogLevel } from 'signalr-uni';

const connection = new HubConnectionBuilder()
    .withUrl("https://example.com/signalr/chat")
    .withAutomaticReconnect([0, 2000, 4000, 8000, 10000, 30000, 60000])
    .configureLogging(LogLevel.Debug)
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

Example (Browser)

let connection = new signalR.HubConnectionBuilder()
    .withUrl("/chat")
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

Example (WebWorker)

importScripts('signalr.js');

let connection = new signalR.HubConnectionBuilder()
    .withUrl("https://example.com/signalr/chat")
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

Example (NodeJS)

// Use `@microsoft/signalr` instead of `signalr-uni` in a NodeJS application.
const signalR = require("@microsoft/signalr");

let connection = new signalR.HubConnectionBuilder()
    .withUrl("/chat")
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

JavaScript and TypeScript clients(compatible with DCloud/uni-app platform) for SignalR for ASP.NET Core and Azure SignalR Service.

Installation

npm install signalr-uni

Usage

See the SignalR Documentation at docs.microsoft.com for documentation on the latest release. API Reference Documentation is also available on docs.microsoft.com.

For documentation on using this client with Azure SignalR Service and Azure Functions, see the SignalR Service serverless developer guide.

UniApp

To use the client in a uni application, install the package to your node_modules folder and use import { /*MODULE NAME*/ } from 'signalr-uni' to load the module.

Browser

To use the client in a browser, copy *.js files from the dist/browser folder to your script folder include on your page using the <script> tag.

WebWorker

To use the client in a webworker, copy *.js files from the dist/webworker folder to your script folder include on your webworker using the importScripts function. Note that webworker SignalR hub connection supports only absolute path to a SignalR hub.

Node.js

To use the client in a NodeJS application, install the package to your node_modules folder and use require('@microsoft/signalr') to load the module. The object returned by require('@microsoft/signalr') has the same members as the global signalR object (when used in a browser).

Example (UniApp)

import { HubConnectionBuilder, LogLevel } from 'signalr-uni';

const connection = new HubConnectionBuilder()
    .withUrl("https://example.com/signalr/chat")
    .withAutomaticReconnect([0, 2000, 4000, 8000, 10000, 30000, 60000])
    .configureLogging(LogLevel.Debug)
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

Example (Browser)

let connection = new signalR.HubConnectionBuilder()
    .withUrl("/chat")
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

Example (WebWorker)

importScripts('signalr.js');

let connection = new signalR.HubConnectionBuilder()
    .withUrl("https://example.com/signalr/chat")
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));

Example (NodeJS)

// Use `@microsoft/signalr` instead of `signalr-uni` in a NodeJS application.
const signalR = require("@microsoft/signalr");

let connection = new signalR.HubConnectionBuilder()
    .withUrl("/chat")
    .build();

connection.on("send", data => {
    console.log(data);
});

connection.start()
    .then(() => connection.invoke("send", "Hello"));
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文