简体   繁体   English

带有ASP.NET和SignalR的井字游戏多人游戏。 创建并将消息发送给群组?

[英]Tic-Tac-Toe Multiplayers with ASP.NET and SignalR. Create and send message to a group?

I've for example this link /Site/Tris/tris.aspx?sessionId=8a657a7b15ee44c39063c8ae45a6ed3b 例如,我有此链接/Site/Tris/tris.aspx?sessionId=8a657a7b15ee44c39063c8ae45a6ed3b

This is the js code: 这是js代码:

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub. 
        var proxy = $.connection.myHub;

        proxy.client.test = function () {
            $('input').hide();//for testing
        };

        // start connetion
        $.connection.hub.start(function () {
            var sessionId = $(document).getUrlParam("sessionId");
            proxy.server.joinGroup(sessionId);
        });
    });
</script>

This is the hub: 这是中心:

 public void JoinGroup(string groupName)
    {
        this.Groups.Add(Context.ConnectionId, groupName);
    }
public void test(string x)
        {
            Clients.Group(x).test();
        }

This is the aspx code when I do a step: 这是我执行步骤时的aspx代码:

 protected void Image_Click(object sender, ImageClickEventArgs e)
    {
        string s = "8a657a7b15ee44c39063c8ae45a6ed3b";

        IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        context.Clients.Group(s).test();
    }

When I call context.Clients.Group(s).test(); 当我调用context.Clients.Group(s).test(); in js goes returns into the $.connection.hub.start and rejoin in the group adn test() is not called! 在js中返回返回到$ .connection.hub.start,然后重新加入组adn test()不会被调用! Why? 为什么? How could I do? 我该怎么办? Thanks 谢谢

The reason was that when I click on an image and there is an event in ASPX, it sends a POST request so the page is refreshed right after 原因是,当我单击图像并在ASPX中发生事件时,它会发送POST请求,因此页面在刷新后立即刷新

 proxy.client.test = function () {
            $('input').hide();//for testing
        };`

is called. 叫做。 I've to do all of this using only js avoid the POST request and the refresh. 我必须使用js来完成所有这些操作,以避免POST请求和刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM