从php到Asp.net的JQuery聊天 [英] JQuery Chat from php to Asp.net

查看:60
本文介绍了从php到Asp.net的JQuery聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在执行从php聊天应用程序到asp.net聊天应用程序的转换.
在php chat应用程序中工作正常,

所以我现在要转换,我从php面临的问题是,他们使用了一些带有会话变量的数组,我不知道Asp.net中的正确对等.这是代码

在php中:

Hi ,

I am doing the conversion from php chat application to asp.net chat application.
In php chat application work fine,

so i am converting now, the problem i am facing from php is, they used some array with session variable i dont know the correct equalent in Asp.net. this is the code

in php:

$_SESSION['openChatBoxes'][$chat['from']] = "from value";



在.net中,我尝试过:



in .net i tried:

DirectCast((Session("openChatBoxes")), ArrayList)(getIndexOf(DirectCast((Session("openChatBoxes")), ArrayList), "fromname")) = "messagecontent"



它为我显示了错误消息,如下所示

索引超出范围.必须为非负数,并且小于集合的大小.参数名称:索引"

请帮我.非常紧急

[修改:添加的代码格式]



Its shows the error message for me as follows

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Please help me. its very urgent

[Modified: added code formatting]

推荐答案

_SESSION [' openChatBoxes']
_SESSION['openChatBoxes']


聊天[' 来自']] = " ;
chat['from']] = "from value";



在.net中,我尝试过:



in .net i tried:

DirectCast((Session("openChatBoxes")), ArrayList)(getIndexOf(DirectCast((Session("openChatBoxes")), ArrayList), "fromname")) = "messagecontent"



它为我显示了错误消息,如下所示

索引超出范围.必须为非负数,并且小于集合的大小.参数名称:索引"

请帮我.非常紧急

[修改:添加的代码格式]



Its shows the error message for me as follows

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Please help me. its very urgent

[Modified: added code formatting]


使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Collections;

公共局部类聊天:System.Web.UI.Page
{
受保护的void Page_Load(对象发送者,EventArgs e)
{

如果(Request.QueryString ["action"]!= null)
{
如果(Session ["chatHistory"] == null)
{
Session ["chatHistory"] = new Hashtable(); ;
}
如果(Session ["openChatBoxes"] == null)
{
Session ["openChatBoxes"] = new Hashtable();
}
如果(Session ["tsChatBoxes"] == null)
{
Session ["tsChatBoxes"] = new Hashtable();
}
开关(Request.QueryString ["action"].ToString())
{
案例"chatheartbeat":
{
chatHeartbeat();
休息;
}
案例"sendchat":
{
sendChat();
休息;
}
案例"closechat":
{
closeChat();
休息;
}
案例"startchatsession":
{
startChatSession();
休息;
}
}
}

Hashtable s = new Hashtable();
}
私人void chatHeartbeat()
{
/*添加代码以从此处的数据库获取聊天,并将结果分配给数据表dtChat */

List< Chattlb> messages = Chating.chat.GetNotRecieved(Session ["username"].ToString());
字符串项目=";

foreach(消息中的Chattlb消息)
{
if(Session ["openChatBoxes"]!= null&& Session ["chatHistory"]!= null)
{
if(!((Hashtable)(Session ["openChatBoxes"]))..包含(message.userfrom)&&(((Hashtable)(Session ["chatHistory"])).Contains(message.userfrom))
{
items =(((Hashtable)(Session ["chatHistory"])))[message.userfrom] .ToString();
}
}

message.message = sanitize(message.message);

items =项目+"\" s \:\" 0 \,\" f \:\" {"+ message.userfrom.ToString()+"} \,\" m \:\" { "+ message.message +"} \";

如果(Session ["chatHistory"]!= null)
{
if(!(((Hashtable)(Session ["chatHistory"]))).包含(message.userfrom))
{
((Hashtable)(Session ["chatHistory"]))[message.userfrom] =";

}
}
如果(Session ["chatHistory"]!= null)
(((Hashtable)(Session ["chatHistory"])))[message.userfrom.ToString()] + ="\" s \:\" 0 \,\" f \:\" {"+消息. userfrom.ToString()+} \",\"m \":\"{" + message.message +} \";
if(Session ["tsChatBoxes"]!= null)
((Hashtable)(Session ["tsChatBoxes"]))[message.userfrom.ToString()] = null; //未设置表示清除会话值
if(Session ["openChatBoxes"]!= null)
((Hashtable)(Session ["openChatBoxes"]))[message.userfrom.ToString()] = message.send;


}
if(Session ["openChatBoxes"]!= null)
{
Hashtable openChatBoxesArray =(Hashtable)(Session ["openChatBoxes"]);
for(int i = 0; i< openChatBoxesArray.Count; i ++)
{
int chatbox = i;
如果(openChatBoxesArray [i]!= null)
{
DateTime时间=(DateTime)(openChatBoxesArray [i]);
字符串timeval =";
TimeSpan Now =(TimeSpan)(DateTime.Now-time);
timeval = time.ToString("g:iA M dS");
字符串消息=发送时间" + timeval;

如果(现在分钟> 180)
{
项目+ ="\" s \:\" 2 \,\" f \:\" {"+ chatbox.ToString()+"} \,\" m \:\" {"+消息+} \";
如果((((Hashtable)(Session ["chatHistory"]))[[chatbox]!= null)
{
((Hashtable)(Session ["chatHistory"]))[chatbox] =";
}
((Hashtable)(Session ["chatHistory"]))[chatbox] + ="\" s \:\" 2 \,\" f \:\" {"+ chatbox.ToString()+"} \,\" m \:\" {"+消息+"} \";
((Hashtable)(Session ["tsChatBoxes"]))[chatbox] ="1";
}
}
}
}
Chating.chat.UpdateRecieved(Session ["username"].ToString());
if(项目!= string.Empty)
{
items = items.Substring(0,items.Length-1);
}
HttpContext.Current.Response.Write("{");
Response.Write("\" items \:[" +项目+]");
HttpContext.Current.Response.Write(}");
Response.End();
}
私有字符串清理(字符串文本)
{
//text = htmlspecialchars(text,ENT_QUOTES); //转换html特殊字符
text = text.Replace("\ n \ r","\ n");
text = text.Replace("\ r \ n","\ n");
text = text.Replace("\ n",< br>");
返回文字;
}
private int getIndexOf(Hashtable SessionArray,字符串值)
{
//尝试
//{
//返回SessionArray.IndexOf(((object)Value);
//}
//catch
//{
返回0;
//}
}

私有字符串chatBoxSession(int chatbox)
{

字符串项目=";

如果((((Hashtable)(Session ["chatHistory"]))[[chatbox]!= null)
{//检查会话变量
items =(((Hashtable)(Session ["chatHistory"])))[chatbox] .ToString();
}

返回项目;
}

私人void sendChat()
{
来自= Session ["username"].ToString();的字符串//将会话用户名值分配给
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class chat : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (Request.QueryString["action"] != null)
{
if (Session["chatHistory"] == null)
{
Session["chatHistory"] = new Hashtable(); ;
}
if (Session["openChatBoxes"] == null)
{
Session["openChatBoxes"] = new Hashtable();
}
if (Session["tsChatBoxes"] == null)
{
Session["tsChatBoxes"] = new Hashtable();
}
switch (Request.QueryString["action"].ToString())
{
case "chatheartbeat":
{
chatHeartbeat();
break;
}
case "sendchat":
{
sendChat();
break;
}
case "closechat":
{
closeChat();
break;
}
case "startchatsession":
{
startChatSession();
break;
}
}
}

Hashtable s = new Hashtable();
}
private void chatHeartbeat()
{
/* Add the Code to get the chat from the databse here and assign the result to datatable dtChat */

List<Chattlb> messages = Chating.chat.GetNotRecieved(Session["username"].ToString());
string items = "";

foreach (Chattlb message in messages)
{
if (Session["openChatBoxes"] != null && Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["openChatBoxes"])).Contains(message.userfrom) && ((Hashtable)(Session["chatHistory"])).Contains(message.userfrom))
{
items = ((Hashtable)(Session["chatHistory"]))[message.userfrom].ToString();
}
}

message.message = sanitize(message.message);

items = items + "\"s\":\"0\",\"f\":\"{" + message.userfrom.ToString() + "}\",\"m\":\"{" + message.message + "}\"";

if (Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["chatHistory"])).Contains(message.userfrom))
{
((Hashtable)(Session["chatHistory"]))[message.userfrom] = "";

}
}
if (Session["chatHistory"] != null)
((Hashtable)(Session["chatHistory"]))[message.userfrom.ToString()] += "\"s\":\"0\",\"f\":\"{" + message.userfrom.ToString() + "}\",\"m\":\"{" + message.message + "}\"";
if (Session["tsChatBoxes"] != null)
((Hashtable)(Session["tsChatBoxes"]))[message.userfrom.ToString()] = null; // Unset means clear the session value
if (Session["openChatBoxes"] != null)
((Hashtable)(Session["openChatBoxes"]))[message.userfrom.ToString()] = message.send;


}
if (Session["openChatBoxes"] != null)
{
Hashtable openChatBoxesArray = (Hashtable)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{
int chatbox = i;
if (openChatBoxesArray[i] != null)
{
DateTime time = (DateTime)(openChatBoxesArray[i]);
string timeval = "";
TimeSpan Now = (TimeSpan)(DateTime.Now - time);
timeval = time.ToString("g:iA M dS");
string message = "Sent at " + timeval;

if (Now.Minutes > 180)
{
items += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{
((Hashtable)(Session["chatHistory"]))[chatbox] = "";
}
((Hashtable)(Session["chatHistory"]))[chatbox] += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
((Hashtable)(Session["tsChatBoxes"]))[chatbox] = "1";
}
}
}
}
Chating.chat.UpdateRecieved(Session["username"].ToString());
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
Response.Write("\"items\":[" + items + "]");
HttpContext.Current.Response.Write(" }");
Response.End();
}
private string sanitize(string text)
{
//text = htmlspecialchars(text, ENT_QUOTES); // Convert the html special characters
text = text.Replace("\n\r", "\n");
text = text.Replace("\r\n", "\n");
text = text.Replace("\n", "<br>");
return text;
}
private int getIndexOf(Hashtable SessionArray, string Value)
{
//try
//{
// return SessionArray.IndexOf((object)Value);
//}
//catch
//{
return 0;
//}
}

private string chatBoxSession(int chatbox)
{

string items = "";

if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{ // Check the Session variable
items = ((Hashtable)(Session["chatHistory"]))[chatbox].ToString();
}

return items;
}

private void sendChat()
{
string from = Session["username"].ToString(); // Assign Session user name value to


这篇关于从php到Asp.net的JQuery聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆