0

我正在尝试为多个用户创建一个页面以相互聊天。所以我偶然发现了这个很棒的教程,它给了我想要的东西。这是链接参考:使用 ASP NET 2.0 和 AJAX 的多用户聊天室

但是,发送按钮未触发任何操作,并且消息未发布在文本框中,如教程中所示。其他所有细节都很好。我真的不知道问题出在哪里,因为我调试时没有显示错误。

这是 Chat.aspx 页面:

<div  onunload="Leave()">
    <input id="hdnRoomID" type="hidden" name="hdnRoomID" runat="server"/>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True" EnablePageMethods="True">
        <Scripts>
            <asp:ScriptReference Path="scripts.js" />
        </Scripts>
    </asp:ScriptManager>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <td width="80%">
               <asp:textbox runat="server" TextMode="MultiLine" id="txt" style="Width: 690px; HEIGHT: 260px" rows="16" Columns="79" ></asp:textbox>
                <br />
               <asp:TextBox id="txtMsg" Width="400" Runat="server"></asp:TextBox>
               <input id="btn" onclick="button_clicked()" type="button" value="SEND" />
            </td>
            <td width="20%" rowspan="2" valign="top">
                Room Members <br />                   
               <asp:ListBox runat="server" Width="100" ID="lstMembers" Enabled="false" Height="249px"></asp:ListBox>                  
            </td>
        </tr>
    </table>
</div>

onclick 发送按钮在 scripts.js 中被触发:

function button_clicked() {
    PageMethods.SendMessage($get("txtMsg").value, $get("hdnRoomID").value, UpdateMessages, errorCallback);
    $get("txtMsg").value = "";
    $get("txt").scrollIntoView("true");
}

function clickButton(e, buttonid) {
    var bt = document.getElementById(buttonid);
    if (typeof bt == 'object') {
        if (navigator.appName.indexOf("Netscape") > (-1)) {
            if (e.keyCode == 13) {
                bt.click();
                return false;
            }
        }

        if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1)) {
            if (event.keyCode == 13) {
                bt.click();
                return false;
            }
        }
    }
}

这里我的聊天的 aspx.cs 代码与 [WebMethods] 包括:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["custName"] == null)
        Response.Redirect("ChatRoom.aspx");
    if (string.IsNullOrEmpty(Request.QueryString["rid"]))
        Response.Redirect("ChatRoom.aspx");

    txtMsg.Attributes.Add("onkeypress", "return clickButton(event,'btn')");
    if (!IsPostBack)
    {
        hdnRoomID.Value = Request.QueryString["rid"];
        ChatRoom room = ChatEngine.GetRoom(hdnRoomID.Value);
        string prevMsgs = room.JoinRoom(Session["custName"].ToString(), Session["custName"].ToString());
        txt.Text = prevMsgs;
        foreach (string s in room.GetRoomUsersNames())
        {
            lstMembers.Items.Add(new ListItem(s, s));
        }

    }


}


#region Script Callback functions

/// <summary>
/// This function is called from the client script 
/// </summary>
/// <param name="msg"></param>
/// <param name="roomID"></param>
/// <returns></returns>
[WebMethod]
static public string SendMessage(string msg, string roomID)
{
    try
    {
        ChatRoom room = ChatEngine.GetRoom(roomID);
        string res = "";
        if (room != null)
        {
            res = room.SendMessage(msg, HttpContext.Current.Session["custName"].ToString());
        }
        return res;
    }
    catch (Exception ex)
    {

    }
    return "";
}


/// <summary>
/// This function is called peridically called from the user to update the messages
/// </summary>
/// <param name="otherUserID"></param>
/// <returns></returns>
[WebMethod]
static public string UpdateUser(string roomID)
{
    try
    {
        ChatRoom room = ChatEngine.GetRoom(roomID);
        if (room != null)
        {
            string res = "";
            if (room != null)
            {
                res = room.UpdateUser(HttpContext.Current.Session["custName"].ToString());
            }
            return res;
        }
    }
    catch (Exception ex)
    {

    }
    return "";
}


/// <summary>
/// This function is called from the client when the user is about to leave the room
/// </summary>
/// <param name="otherUser"></param>
/// <returns></returns>
[WebMethod]
static public string LeaveRoom(string roomID)
{
    try
    {
        ChatRoom room = ChatEngine.GetRoom(roomID);
        if (room != null)
            room.LeaveRoom(HttpContext.Current.Session["custName"].ToString());
    }
    catch (Exception ex)
    {

    }
    return "";
}


/// <summary>
/// Returns a comma separated string containing the names of the users currently online
/// </summary>
/// <param name="roomID"></param>
/// <returns></returns>
[WebMethod]
static public string UpdateRoomMembers(string roomID)
{
    try
    {
        ChatRoom room = ChatEngine.GetRoom(roomID);
        if (room != null)
        {
            IEnumerable<string> users = room.GetRoomUsersNames();
            string res = "";

            foreach (string s in users)
            {
                res += s + ",";
            }
            return res;
        }
    }
    catch (Exception ex)
    {
    }
    return "";
}
#endregion

}

4

0 回答 0