Javascript .net core 2.0信号器为什么我不能使用用户名发送

Javascript .net core 2.0信号器为什么我不能使用用户名发送,javascript,c#,asp.net-core,signalr,asp.net-core-signalr,Javascript,C#,Asp.net Core,Signalr,Asp.net Core Signalr,我一直在尝试使用asp.NETCore2.0和signalr制作一个象形游戏。 我遇到的问题是,由于某些原因,我无法使用组,因为它不会发送回客户端。所以我决定组建自己的团队 我正在使用.NETCore的身份验证,并尝试使用用户名发送到客户端。但没有什么会回到客户那里 下面是我用来保存连接用户信息的viewmodel。 我曾尝试从信号器集线器使用connectionid,但这种情况一直在变化,因此它一直使用错误的id发送 public class GameLobbyViewModel {

我一直在尝试使用asp.NETCore2.0和signalr制作一个象形游戏。 我遇到的问题是,由于某些原因,我无法使用组,因为它不会发送回客户端。所以我决定组建自己的团队

我正在使用.NETCore的身份验证,并尝试使用用户名发送到客户端。但没有什么会回到客户那里

下面是我用来保存连接用户信息的viewmodel。 我曾尝试从信号器集线器使用connectionid,但这种情况一直在变化,因此它一直使用错误的id发送

    public class GameLobbyViewModel
{
    public string ID { get; set; }

    public string LobbyName { get; set; }

    public string[] PlayerNames { get; set; } = new String[4];

    public string[] PlayerConnectionId { get; set; } = new String[4];

    public string DrawWord { get; set; }

    public int[] Points { get; set; } = new int[4];
}
写入viewmodel的控制器操作:

public IActionResult Game(string id)
    {
        var lobby = _lobbyManager.GetLobby(id);
        if (lobby == null)
            return RedirectToAction("Index"); //redirect back to homepage for refresh of the lobby list

        //Name of the loggedin user
        string playerName = User.Identity.Name;

        bool spot = false;
        for (int i = 0; i < lobby.PlayerNames.Length; ++i)
        {
            if (String.IsNullOrEmpty(lobby.PlayerNames[i]))
            {
                //make sure that the same player cant be shown twice.
                if (lobby.PlayerNames.Contains(playerName))
                {
                    spot = true;
                    break;
                }
                else
                {
                    lobby.PlayerNames[i] = playerName;
                    lobby.PlayerConnectionId[i] = _signalRHub.Context.ConnectionId;
                    spot = true;
                    break;
                }
            }
        }

        if (!spot) // this game is full...
            return RedirectToAction("index");

        return View(lobby);
    }
调用draw和接收在画布上绘制的javascript文件

    var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 500;
var canvasX = $(canvas).offset().left;
var canvasY = $(canvas).offset().top;
var last_mouseX = last_mouseY = 0;
var mouseX = mouseY = 0;
var mousedown = false;
var tooltype = 'draw';
var radius = 2;
var clr;
//makes a connection to drawhub.
var connection = new signalR.HubConnection('/draw');

//gets the mouse positions when you click
$(canvas).on('mousedown', function (e) {
    last_mouseX = mouseX = parseInt(e.clientX - canvasX);
    last_mouseY = mouseY = parseInt(e.clientY - canvasY);
    mousedown = true;
});

//duh
$(canvas).on('mouseup', function (e) {
    mousedown = false;
});

//this draws a line from the last mousepos to the current
//takes last positions current, color and radiuis of line
var drawCanvas = function (prev_x, prev_y, x, y, clr, radius) {
    ctx.beginPath();
    ctx.globalCompositeOperation = 'source-over';
    ctx.fillStyle = clr;
    ctx.strokeStyle = clr;
    ctx.lineWidth = radius;
    ctx.moveTo(prev_x, prev_y);
    ctx.lineTo(x, y);
    ctx.lineJoin = ctx.lineCap = 'round';
    ctx.stroke();
};

//clears the cansvas
var clear = function ()
{
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    console.log('clearedededed');
}

//does stuff when you move the mouse
$(canvas).on('mousemove', function (e) {
    mouseX = parseInt(e.clientX - canvasX);
    mouseY = parseInt(e.clientY  - canvasY);

    if ((last_mouseX > 0 && last_mouseY > 0) && mousedown)
    {
        drawCanvas(mouseX, mouseY, last_mouseX, last_mouseY, clr, radius);
        //invoke draw function method thingy from the draw hub.
        var lobbyName = $('#lobbyName').val();
        connection.invoke('draw', last_mouseX, last_mouseY, mouseX, mouseY, clr, radius, lobbyName);
    }
    last_mouseX = mouseX;
    last_mouseY = mouseY;
});
//looks if you click the clear button and then invokes the clear method funciton thingy from draw hub.
document.getElementById("clearCanvas")
    .addEventListener('click', function () {
        var lobbyId = $('#lobbyName').val();
        connection.invoke('clear', lobbyId);
    });

var mouse_down = false;

//when the connection returns draw runs the draw funcion with all positions and color and radius received from hub.
connection.on('draw', function (prev_x, prev_y, x, y, clr, radius)
{
    console.log("drawn");
    drawCanvas(prev_x, prev_y, x, y, clr, radius);
});
//when connection returns clear runs the clear function
connection.on('clear', function () {
    clear();
});

connection.on('test', function () {
    console.log("Test");
});

//start the connection.
connection.start();

非常感谢您对我的帮助。

客户端。User
接受的是
用户ID
,而不是姓名

客户端。客户端
接收连接ID:

foreach (var item in lobby.PlayerConnectionId)
{
 // var playerConnection = lobby.PlayerNames[i];
 // await Clients.User(playerConnection)
    await Clients.Client(item)
        .SendAsync("draw", prevX, prevY, currentX, currentY, color, radius);
}

谢谢你的回答。我使用连接id的唯一问题是它改变了allot,所以我用userId测试了它,它似乎可以工作。
foreach (var item in lobby.PlayerConnectionId)
{
 // var playerConnection = lobby.PlayerNames[i];
 // await Clients.User(playerConnection)
    await Clients.Client(item)
        .SendAsync("draw", prevX, prevY, currentX, currentY, color, radius);
}