安装
npm install @microsoft/signalr
使用
const signalR = require("@microsoft/signalr");
const connection = new signalR.HubConnectionBuilder()
  .withUrl("http://localhost:5000/chathub", {})
  .configureLogging(signalR.LogLevel.Error)  
  .build();
  
 
 
connection.on("RecieveMessage", (user, message) => {
  console.log(user, message);
});
connection.on("RecieveMessage", data => {
  let [user, message] = data;
  console.log(user, message);
});
connection.onreconnecting(error => {
  console.log(error);
});
connection.onreconnected(connectionId => {
  console.log(connectionId);
});
connection.onclose(error => {
  console.log(error);
});
async start() {
    try {
        await connection.start();
        console.log("SignalR Connected.");
    } catch (err) {
        console.log(err);
        setTimeout(this.start(), 5000);
    }
}
start()
start()
  .then(()=>connection.invoke("MethodName", params))
  .catch(error => console.log(error));
Home.vue
<template>
  <div>
    <input v-model="user" type="text" />
    <input v-model="message" type="text" /><br />
    <button @click="sendMsg">发送</button>
    <hr />
    <ul>
      <li v-for="(item, index) in msgList" :key="index">
        {{ item.user }}:    {{ item.msg }}
      </li>
    </ul>
  </div>
</template>
<script>
const signalR = require("@microsoft/signalr");
export default {
  data() {
    return {
      connection: "",
      user: "",
      message: "",
      msgList: []
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.connection = new signalR.HubConnectionBuilder()
        .withUrl("http://localhost:5000/chathub", {})
        .configureLogging(signalR.LogLevel.Error)
        .build();
      this.connection.on("ReceiveMessage", data => {
        this.msgList.push(data);
      });
      this.connection.start();
    },
    sendMsg() {
      let params = {
        user: this.user,
        message: this.message
      };
      this.connection.invoke("SendMessage", params);
    }
  }
};
</script>
<style></style>
日志等级
signalR.LogLevel.Error:错误消息。 Error仅记录消息。
signalR.LogLevel.Warning:有关潜在错误的警告消息。 日志 Warning 和 Error 消息。
signalR.LogLevel.Information:无错误的状态消息。 日志 Information 、 Warning 和 Error 消息。
signalR.LogLevel.Trace:跟踪消息。 记录所有内容,包括中心和客户端之间传输的数据。
客户端调用服务端
connection.invoke("SendMessage", ...params);
connection.invoke("SendMessage", user, message);
服务端调用客户端
connection.on("ReceiveMessage",data=>{
    console.log(data)
    
});
服务器端代码示例
public async Task SendMessage(string user, string message)
{
   
    await Clients.All.SendAsync("ReceiveMessage", data);
}
await Clients.All.SendAsync("ReceiveMessage", data);
await Clients.Group("Users").SendAsync("ReceiveMsg", data);
await Clients.Caller.SendAsync("ReceiveMessage", data);
await Clients.Others.SendAsync("ReceiveMessage", data);
await Clients.User(connectionId).SendAsync("ReceiveMessage", data);
await Clients.Client(connectionId).SendAsync("ReceiveMessage", data);
await Clients.Clients(IReadOnlyList<> connectionIds).SendAsync("ReceiveMessage", data);