using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Web.Security;
using System.Configuration;
[WebService(Namespace="http://tempuri.org/"), WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1), Microsoft.VisualBasic.CompilerServices.DesignerGenerated(), System.Web.Script.Services.ScriptService()]
public class ChatService : System.Web.Services.WebService
{
[WebMethod()]
public string GetMessages(string strFromUserID, string strToUserID)
{
bool blnSucess = false;
string strMessage = null;
DateTime dDateSent = DateTime.MinValue;
strMessage = "";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strFromUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strFromUserID));
strFromUserGUID = MemUser.ProviderUserKey.ToString();
string strToUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strToUserID));
strToUserGUID = MemUser.ProviderUserKey.ToString();
cmd = new SqlCommand("SELECT TOP(1) Message, DateSent FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID) ORDER BY DateSent DESC", conn);
cmd.Parameters.AddWithValue("@SenderUserID", strToUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strFromUserGUID);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dDateSent = (DateTime)reader["DateSent"];
strMessage = (string)reader["Message"];
blnSucess = true;
}
reader.Close();
cmd.Dispose();
if (blnSucess)
{
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strToUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strFromUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
return strMessage;
}
[WebMethod()]
public string SendMessage(string strSenderUserID, string strRecipientUserID, string strMessage)
{
bool blnSucess = false;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO IMChats(RecipientUserID, SenderUserID, Message, DateSent) VALUES (@RecipientUserID, @SenderUserID, @Message, @DateSent)";
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@Message", strMessage);
cmd.Parameters.AddWithValue("@DateSent", DateTime.Now);
cmd.ExecuteNonQuery();
blnSucess = true;
cmd.Dispose();
conn.Close();
}
return blnSucess;
}
[WebMethod()]
public string CheckChatReq(string strMyUserID)
{
string strSenderUserID = null;
string strMessage = null;
DateTime dDateSent = DateTime.MinValue;
strSenderUserID = "";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
string strMyUserGUID = "";
string strSenderUserGUID = "";
MembershipUser MemUser = Membership.GetUser(HttpUtility.UrlDecode(strMyUserID));
strMyUserGUID = MemUser.ProviderUserKey.ToString();
cmd = new SqlCommand("SELECT TOP(1) SenderUserID, Message, DateSent FROM [IMChats] WHERE ([RecipientUserID] = @RecipientUserID) ORDER BY DateSent DESC", conn);
cmd.Parameters.AddWithValue("@RecipientUserID", strMyUserGUID);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dDateSent = (DateTime)reader["DateSent"];
strMessage = (string)reader["Message"];
if (strMessage == "~::::=[(HANDSHAKE)]=::::~[ENQ]")
{
strSenderUserGUID = reader["SenderUserID"].ToString();
strSenderUserID = Membership.GetUser(new Guid(strSenderUserGUID)).UserName;
}
}
reader.Close();
cmd.Dispose();
if (! (string.IsNullOrEmpty(strSenderUserGUID)))
{
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strMyUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
return strSenderUserID;
}
[WebMethod()]
public bool SendChatReq(string strSenderUserID, string strRecipientUserID)
{
bool blnSucess = false;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @RecipientUserID AND [RecipientUserID] = @SenderUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO IMChats(RecipientUserID, SenderUserID, Message, DateSent) VALUES (@RecipientUserID, @SenderUserID, @Message, @DateSent)";
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@Message", "~::::=[(HANDSHAKE)]=::::~[ENQ]");
cmd.Parameters.AddWithValue("@DateSent", DateTime.Now);
cmd.ExecuteNonQuery();
blnSucess = true;
cmd.Dispose();
conn.Close();
}
return blnSucess;
}
[WebMethod()]
public string ChatCleanUpAll(string strMyUserID)
{
bool blnSucess = false;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
string strMyUserGUID = null;
MembershipUser MemUser = Membership.GetUser(HttpUtility.UrlDecode(strMyUserID));
strMyUserGUID = MemUser.ProviderUserKey.ToString();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@RecipientUserID", strMyUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
blnSucess = true;
}
return blnSucess;
}
[WebMethod()]
public bool CleanUp(string strMyUserID, string strSenderUserID, bool blnSendEOT)
{
bool blnSucess = false;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
string strMyUserGUID = null;
MembershipUser MemUser = Membership.GetUser(HttpUtility.UrlDecode(strMyUserID));
strMyUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@RecipientUserID", strMyUserGUID);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
if (blnSendEOT)
{
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO IMChats(RecipientUserID, SenderUserID, Message, DateSent) VALUES (@RecipientUserID, @SenderUserID, @Message, @DateSent)";
cmd.Parameters.AddWithValue("@RecipientUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@SenderUserID", strMyUserGUID);
cmd.Parameters.AddWithValue("@Message", "~::::=[(HANDSHAKE)]=::::~[EOT]");
cmd.Parameters.AddWithValue("@DateSent", DateTime.Now);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
blnSucess = true;
}
return blnSucess;
}
[WebMethod()]
public bool SendNak(string strSenderUserID, string strRecipientUserID)
{
bool blnSucess = false;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO IMChats(RecipientUserID, SenderUserID, Message, DateSent) VALUES (@RecipientUserID, @SenderUserID, @Message, @DateSent)";
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@Message", "~::::=[(HANDSHAKE)]=::::~[NAK]");
cmd.Parameters.AddWithValue("@DateSent", DateTime.Now);
cmd.ExecuteNonQuery();
blnSucess = true;
cmd.Dispose();
conn.Close();
}
return blnSucess;
}
[WebMethod()]
public bool SendAck(string strSenderUserID, string strRecipientUserID)
{
bool blnSucess = false;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO IMChats(RecipientUserID, SenderUserID, Message, DateSent) VALUES (@RecipientUserID, @SenderUserID, @Message, @DateSent)";
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@Message", "~::::=[(HANDSHAKE)]=::::~[ACK]");
cmd.Parameters.AddWithValue("@DateSent", DateTime.Now);
cmd.ExecuteNonQuery();
blnSucess = true;
cmd.Dispose();
conn.Close();
}
return blnSucess;
}
[WebMethod()]
public bool GetAck(string strRecipientUserID, string strSenderUserID)
{
bool blnSucess = false;
string strMessage = null;
DateTime dDateSent = DateTime.MinValue;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = new SqlCommand("SELECT TOP(1) Message, DateSent FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID) ORDER BY DateSent DESC", conn);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dDateSent = (DateTime)reader["DateSent"];
strMessage = (string)reader["Message"];
if (strMessage == "~::::=[(HANDSHAKE)]=::::~[ACK]")
{
blnSucess = true;
}
}
reader.Close();
cmd.Dispose();
if (blnSucess)
{
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
return blnSucess;
}
[WebMethod()]
public bool GetNak(string strRecipientUserID, string strSenderUserID)
{
bool blnSucess = false;
string strMessage = null;
DateTime dDateSent = DateTime.MinValue;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = new SqlCommand("SELECT TOP(1) Message, DateSent FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID) ORDER BY DateSent DESC", conn);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dDateSent = (DateTime)reader["DateSent"];
strMessage = (string)reader["Message"];
if (strMessage == "~::::=[(HANDSHAKE)]=::::~[NAK]")
{
blnSucess = true;
}
}
reader.Close();
cmd.Dispose();
if (blnSucess)
{
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
return blnSucess;
}
[WebMethod()]
public bool SendEot(string strSenderUserID, string strRecipientUserID)
{
bool blnSucess = false;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @RecipientUserID AND [RecipientUserID] = @SenderUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO IMChats(RecipientUserID, SenderUserID, Message, DateSent) VALUES (@RecipientUserID, @SenderUserID, @Message, @DateSent)";
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@Message", "~::::=[(HANDSHAKE)]=::::~[EOT]");
cmd.Parameters.AddWithValue("@DateSent", DateTime.Now);
cmd.ExecuteNonQuery();
blnSucess = true;
cmd.Dispose();
conn.Close();
}
return blnSucess;
}
[WebMethod()]
public bool GetEot(string strRecipientUserID, string strSenderUserID)
{
bool blnSucess = false;
string strMessage = null;
DateTime dDateSent = DateTime.MinValue;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = null;
MembershipUser MemUser = null;
string strRecipientUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strRecipientUserID));
strRecipientUserGUID = MemUser.ProviderUserKey.ToString();
string strSenderUserGUID = null;
MemUser = Membership.GetUser(HttpUtility.UrlDecode(strSenderUserID));
strSenderUserGUID = MemUser.ProviderUserKey.ToString();
cmd = new SqlCommand("SELECT TOP(1) Message, DateSent FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID) ORDER BY DateSent DESC", conn);
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dDateSent = (DateTime)reader["DateSent"];
strMessage = (string)reader["Message"];
if (strMessage == "~::::=[(HANDSHAKE)]=::::~[EOT]")
{
blnSucess = true;
}
}
reader.Close();
cmd.Dispose();
if (blnSucess)
{
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM [IMChats] WHERE ([SenderUserID] = @SenderUserID AND [RecipientUserID] = @RecipientUserID)";
cmd.Parameters.AddWithValue("@SenderUserID", strSenderUserGUID);
cmd.Parameters.AddWithValue("@RecipientUserID", strRecipientUserGUID);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
return blnSucess;
}
}
in the code above the error is cannot implicitly convert type bool to string