.net core 2.0 signalr 为什么我不能使用用户名发送

.net core 2.0 signalr why can I not Send using username

我一直在尝试使用 asp.net core 2.0 和 signalr 制作一个画图游戏。 我遇到的问题是由于某种原因我无法使用组,因为它不会发送回客户端。所以我决定自己组。

我正在使用 .net core 的身份验证,并一直在尝试使用用户名发送给客户端。但没有任何东西返回给客户。

这是我用来保存已连接用户信息的视图模型。 我尝试使用信号器中心的 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];
}

写入视图模型的控制器操作:

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);
    }

这是我的集线器方法,我将绘图发送给客户端我在集线器中所做的一切都与此几乎相同。

public async Task Draw(int prevX, int prevY, int currentX, int currentY, string color, int radius, string groupName)
    {
        var lobby = _lobbyManager.GetLobby(groupName);
        int i = 0;
        foreach (var item in lobby.PlayerConnectionId)
        {
            var playerConnection = lobby.PlayerNames[i];
            if (playerConnection == null)
            {
                break;
            }
            else
            {
                await Clients.User(playerConnection).SendAsync("draw", prevX, prevY, currentX, currentY, color, radius);
                i++;
            }
        }
    }

javascript 文件,它在其中调用绘图以及在 canvas.

上接收绘图的位置
    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();

如能帮助我实现这一点,我将不胜感激。

Clients.User 接受 userId,而不是名字。

Clients.Client 接受 connectionId:

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);
}