我已经能够在我的网站上添加聊天功能,但我想知道是否有更有效的方法来实现它。这是聊天的当前结构:
带有javascript函数的HTML页面:
sendChat(elmnt) {
var result = XHR("http_sendChat.aspx","POST",0,elmnt); //xmlhttprequest to send chat message by posting elmnt string to page. 0 used for not polling
}
getChat() {
var result = XHR("http_getChat.aspx","GET",50,""); //polls page every 50ms to get new chat messages
addMessageElmnt(result); //update chat window with new messages
}
在vb.net的服务器端http_sendChat.aspx:
Application.Lock
Application("chat") = Application("chat") & Request.Form("message") //Global application object stores chat log
Application.Unlock
在vb.net的服务器端http_getChat.aspx:
Dim chatTemp
chatTemp = Mid(Application("chat"),Session("chatIndex")) //fetches whatever chat data hasn't been fetched yet
Session("chatIndex") = Session("chatIndex") + Len(chatTemp) //set index to last read position
Response.Write(chatTemp)
还有一些代码主要检查以确保用户帐户已激活等,但就聊天而言,还有更好的方法吗?我问,因为当有100人登录并使用聊天时它相当慢。