在即时消息方面需要帮助 [英] Need Help Regarding Instant Messaging

查看:63
本文介绍了在即时消息方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个社交网站,我想在其中实现即时通讯工具来与用户进行沟通.

我搜索了帮助,但发现了旧的场景,人们通过聊天室进行交流.

相反,我需要一个gtalk或Facebook之类的应用程序,其中1个人可以从他的朋友列表中与在线朋友进行交流.

请同样指导我.

谢谢您.

I''m developing a social networking site where i want to implement instant messenger to communicate the user.

I searched for the help but i found the old scenario where the people communicating via chat rooms.

Rather i wan a application like gtalk or the facebook where 1 person can communicate with the online friends from his friend list.

Kindly guide me for the same.

Thanking you

推荐答案

它与聊天室基本相同,但是您可以为对话创建一个临时的房间".这样一来,Gtalk便很容易做到,它允许您邀请更多的人进行双向聊天来创建群聊.

至于实现它,您需要一个计时器驱动的AJAX轮询器,该轮询器询问服务器是否有任何更改,并在用户键入某些内容,更改状态或在浏览器中显示与聊天相关的任何其他操作时提交AJAX提交.我想您可以在其中找到相应的说明.
It''s basically the same as a chatroom, but you can create a temporary ''room'' for the conversation. That makes it easy to do that thing Gtalk does of allowing you to create a group chat by inviting more people into a two-way chat.

As for implementing it, you need a timer-driven AJAX poller which asks the server whether anything has changed, and make AJAX submissions when a user types something, changes status or does anything else relevant to chat display in their browser. I imagine you can find instructions on that easily enough.


选中此
http://stackoverflow.com/questions/757536/integrating-instant- messages-into-an-asp-net-application [ ^ ]
Check this
http://stackoverflow.com/questions/757536/integrating-instant-messaging-into-an-asp-net-application[^]


感谢,我粘贴了登录页面和聊天室的代码,请建议我使用javascript加载每个用户的联系和聊天窗口.


Default.aspx.vb


thanx, im pasting the code for the log in page and chatroom , please suggest me the javascript to load the contact and also to chat windows per user.


Default.aspx.vb


Imports System
Imports System.Linq
Imports System.Web.UI.WebControls

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
        Dim db As LinqChatDataContext = New LinqChatDataContext

        Dim user = (From u In db.Users _
                   Where u.Username = Login1.UserName _
                   And u.Password = Login1.Password _
                   Select u).SingleOrDefault()

        If Not user Is Nothing Then
            e.Authenticated = True
            Session("ChatUserID") = user.UserID
            Session("ChatUsername") = user.Username
        Else
            e.Authenticated = False
        End If
    End Sub

    Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
        Response.Redirect("Chatroom.aspx?roomId=1")
    End Sub
End Class









chatroom.aspx.vb









chatroom.aspx.vb

Imports System
Imports System.Web.UI.WebControls
Imports System.Linq

Partial Class Chatroom
    Inherits System.Web.UI.Page
    Implements System.Web.UI.ICallbackEventHandler

    Private _callBackStatus As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ' for simplity's sake we're going to assume that a
            ' roomId was passed in the query string and that
            ' it is an integer
            ' note: in reality you would check if the roomId is empty
            ' and is an integer
            Dim roomId = Request("roomId").ToString()
            lblRoomId.Text = roomId

            Me.GetRoomInformation()
            Me.GetLoggedInUsers()
            Me.InsertMessage(ConfigurationManager.AppSettings("ChatLoggedInText") + " " + DateTime.Now.ToString())
            Me.GetMessages()

            ' create a call back reference so we can log-out user when user closes the browser
            Dim callBackReference As String = Page.ClientScript.GetCallbackEventReference(Me, "arg", "LogOutUser", "")
            Dim logOutUserCallBackScript As String = "function LogOutUserCallBack(arg, context) { " & callBackReference & "; }"
            Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), "LogOutUserCallBack", logOutUserCallBackScript, True)
        End If
    End Sub

    Private Sub GetRoomInformation()
        ' get the room information from the database
        ' we() 're going to set this up so that we can use
        ' many rooms if we want to
        Dim db As LinqChatDataContext = New LinqChatDataContext()

        Dim room = (From r In db.Rooms _
                   Where r.RoomID = Convert.ToInt32(lblRoomId.Text) _
                   Select r).SingleOrDefault()

        lblRoomName.Text = room.Name
    End Sub

    Private Sub GetLoggedInUsers()
        Dim db As LinqChatDataContext = New LinqChatDataContext()

        ' let's check if this authenticated user exist in the
        ' LoggedInUser table (means user is logged-in to this room)
        Dim user = (From u In db.LoggedInUsers _
                    Where u.UserID = Convert.ToInt32(Session("ChatUserID")) _
                    And u.RoomID = Convert.ToInt32(lblRoomId.Text) _
                    Select u).SingleOrDefault()

        ' if user does not exist in the LoggedInUser table
        ' then let's add/insert the user to the table
        If user Is Nothing Then
            Dim loggedInUser As LoggedInUser = New LoggedInUser()
            loggedInUser.UserID = Convert.ToInt32(Session("ChatUserID"))
            loggedInUser.RoomID = Convert.ToInt32(lblRoomId.Text)
            db.LoggedInUsers.InsertOnSubmit(loggedInUser)
            db.SubmitChanges()
        End If

        Dim userIcon As String
        Dim sb As StringBuilder = New StringBuilder()

        ' get all logged in users to this room
        Dim loggedInUsers = From l In db.LoggedInUsers _
                            Where l.RoomID = Convert.ToInt32(lblRoomId.Text) _
                            Select l

        For Each loggedInUser In loggedInUsers
            ' show user icon based on sex
            If loggedInUser.User.Sex.ToString().ToLower() = "m" Then
                userIcon = "<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''>  "
            Else
                userIcon = "<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  "
            End If

            If (Not loggedInUser.User.Username = Session("ChatUsername").ToString()) Then
                sb.Append(userIcon + "<a href="#">" + loggedInUser.User.Username + "</a><br>")
            Else
                sb.Append(userIcon + "" + loggedInUser.User.Username + "<br>")
            End If
        Next

        ' holds the names of the users shown in the chatroom
        litUsers.Text = sb.ToString()
    End Sub

    ''' <summary>
    ''' This will insert the passed text to the message table in the database
    ''' </summary>
    Private Sub InsertMessage(ByVal text As String)
        Dim db As LinqChatDataContext = New LinqChatDataContext()

        Dim message As Message = New Message()
        message.RoomID = Convert.ToInt32(lblRoomId.Text)
        message.UserID = Convert.ToInt32(Session("ChatUserID"))

        If String.IsNullOrEmpty(text) Then
            message.Text = txtMessage.Text.Replace("<", "")
            message.Color = ddlColor.SelectedValue
        Else
            message.Text = text
            message.Color = "gray"
        End If

        message.ToUserID = Nothing            ' in the future, we will use this value for private messages
        message.TimeStamp = DateTime.Now
        db.Messages.InsertOnSubmit(message)
        db.SubmitChanges()
    End Sub

    ''' <summary>
    ''' Get the last 20 messages for this room
    ''' </summary>
    Private Sub GetMessages()
        Dim db As LinqChatDataContext = New LinqChatDataContext()

        Dim messages = (From m In db.Messages _
                        Where m.RoomID = Convert.ToInt32(lblRoomId.Text) _
                        Order By m.TimeStamp Descending _
                        Select m).Take(20).OrderBy(Function(m As Message) m.TimeStamp)

        'Select m).Take(20).OrderBy(m >= m.TimeStamp)

        If Not messages Is Nothing Then
            Dim sb As StringBuilder = New StringBuilder()
            Dim ctr As Integer = 0    ' toggle counter for alternating color

            For Each message In messages
                ' alternate background color on messages
                If ctr = 0 Then
                    sb.Append("<div style="padding: 10px;">")
                    ctr = 1
                Else
                    sb.Append("<div style="background-color: #EFEFEF; padding: 10px;">")
                    ctr = 0
                End If

                If message.User.Sex.ToString().ToLower() = "m" Then
                    sb.Append("<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>")
                Else
                    sb.Append("<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>")
                End If
            Next

            litMessages.Text = sb.ToString()
        End If
    End Sub

    Protected Sub BtnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If txtMessage.Text.Length > 0 Then
            Me.InsertMessage(Nothing)
            Me.GetMessages()
            txtMessage.Text = String.Empty
            ScriptManager1.SetFocus(txtMessage.ClientID)
        End If
    End Sub

    Protected Sub Timer1_OnTick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.GetLoggedInUsers()
        Me.GetMessages()

        If Session("IsChatroomInFocus") Is Nothing Then
            ScriptManager1.SetFocus(txtMessage)
        End If
    End Sub

    Protected Sub BtnLogOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogOut.Click
        ' log out the user by deleting from the LoggedInUser table
        Dim db As LinqChatDataContext = New LinqChatDataContext()

        Dim loggedInUser = (From l In db.LoggedInUsers _
                            Where l.UserID = Convert.ToInt32(Session("ChatUserID")) _
                            And l.RoomID = Convert.ToInt32(lblRoomId.Text) _
                            Select l).SingleOrDefault()

        db.LoggedInUsers.DeleteOnSubmit(loggedInUser)
        db.SubmitChanges()

        ' insert a message that this user has logged out
        Me.InsertMessage("Just logged out! " + DateTime.Now.ToString())

        ' clean the session
        Session.RemoveAll()
        Session.Abandon()

        ' redirect the user to the login page
        Response.Redirect("Default.aspx")
    End Sub

    Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
        Return _callBackStatus
    End Function

    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        _callBackStatus = "failed"

        ' log out the user by deleting from the LoggedInUser table
        Dim db As LinqChatDataContext = New LinqChatDataContext()

        Dim loggedInUser = (From l In db.LoggedInUsers _
                            Where l.UserID = Convert.ToInt32(Session("ChatUserID")) _
                            And l.RoomID = Convert.ToInt32(lblRoomId.Text) _
                            Select l).SingleOrDefault()

        db.LoggedInUsers.DeleteOnSubmit(loggedInUser)
        db.SubmitChanges()

        ' insert a message that this user has logged out
        Me.InsertMessage("Just logged out! " + DateTime.Now.ToString())

        _callBackStatus = "success"
    End Sub
End Class
</br></br>


这篇关于在即时消息方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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