1

下面是我的 .aspx 页面代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="chat.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.6.4.js" type="text/javascript"></script>
    <script src="Scripts/json2.js" type="text/javascript"></script>
    <script src="Scripts/jquery.signalR-1.0.0-rc2.min.js" type="text/javascript"></script>
    <script src="signalr/hubs" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var query = window.location.search;
            var toRemove = '?id=';
            var gorge = query.replace(toRemove, '');
            // Proxy created on the fly
            var hub = $.connection.chatHub;
            $.connection.hub.qs = "Id=" + gorge;
            // Start the connection
            $.connection.hub.start(function () {
                //chat.server.getAllOnlineStatus();
            });
        });
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container" class="wrap">
        <div id="chatbox" class="chatbox">
            <ul id="frndcontact">
            </ul>
        </div>
    </div>
    </form>
</body>
</html>

下面是我的 Hub 类

[HubName("chatHub2")]

public class Chat2 : Hub
{
    private SqlConnection objconn;
    string Connstr = @"Data Source=somevalue;Initial Catalog=somevalue;Integrated Security=True;";
    public Task JoinGroup()
    {
        return Groups.Add(Context.ConnectionId, "foo");
    }
    public DataTable GetDataTable(string strQuery)
    {
        SqlCommand objcmd;
        SqlDataAdapter objda;
        DataTable ds = new DataTable();

        try
        {
            objconn = new SqlConnection(Connstr);
            objconn.Open();
            objcmd = new SqlCommand(strQuery, objconn);
            objda = new SqlDataAdapter(objcmd);

            objda.Fill(ds);
            return ds;
        }
        catch (SqlException ex)
        {
            throw ex.InnerException;
        }
        finally
        {
            objconn.Close();
            objcmd = null;
            objda = null;
        }
    }

    public override Task OnConnected()
    {
        int id = Convert.ToInt32(Context.QueryString["id"]);
        string sql = string.Format("exec getfriend '" + id + "' ");
        System.Data.DataTable dtgetfriend = GetDataTable(sql);      
    }}

现在,当我调试时,我无法获得断点OnConnected。为什么我不能从这段代码开始?

我也在 global.asax 中添加了这段代码

 public void Application_Start()
 {
     RouteTable.Routes.MapHubs();
 }
4

1 回答 1

5

您没有订阅中心。在开始之前,您需要声明至少一个客户端函数,并且您需要通过正确的名称引用您的集线器。因此,如果您将 JS 修改为:

$(document).ready(function () {
        var query = window.location.search;
        var toRemove = '?id=';
        var gorge = query.replace(toRemove, '');
        // Proxy created on the fly
        var hub = $.connection.chatHub2;
        $.connection.hub.qs = "Id=" + gorge;

        hub.client.foo = function() {};
        // Start the connection
        $.connection.hub.start(function () {
            //chat.server.getAllOnlineStatus();
        });
    });

你的身体会很好。

于 2013-04-27T05:02:45.797 回答