65.9K
CodeProject 正在变化。 阅读更多。
Home

多协议聊天

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.56/5 (8投票s)

2005年3月23日

GPL3
viewsIcon

38771

downloadIcon

1579

使用此应用程序,您可以与计算机和不同的协议进行聊天。

引言

此应用程序是一个简单的聊天应用程序,它提供了使用不同协议(IP、串口、调制解调器、IPX、...)的方式。

代码块

Module1:

如果您愿意,您可以开发一个与聊天兼容的插件(例如游戏)来使用它。

Public DX As New DirectX7
Public DP As DirectPlay4
Public DPEnum As DirectPlayEnumConnections
Public DPAddress As DirectPlayAddress
Public Session As DirectPlaySessionData
Public DPEnumSessions As DirectPlayEnumSessions

Public PlayerIDNum As Long
Public PlayerFriendly As String
Public PlayerFormal As String

'Warning: Don't use the string below for the chat.
Public Const MyGuid As String = _
      "{8EC1E2EC-5266-11D4-811C-AD15B9B82C76}"

Public Sub CreateSession(ByRef ConnectionIndex As Long)
  'with this we create a session.
  Set DPAddress = DPEnum.GetAddress(ConnectionIndex)
  Call DP.InitializeConnection(DPAddress)

  Set Session = DP.CreateSessionData
  Session.SetMaxPlayers 8 'here are only 8 chatters/players allowed
  Session.SetSessionName "ChatSession"
  Session.SetGuidApplication MyGuid 'the chat uses the created GUID
  'these 2 flags should be enough for every application.
  Session.SetFlags _
        DPSESSION_DIRECTPLAYPROTOCOL Or DPSESSION_MIGRATEHOST

  'now we create a session based on the properties we've just set.
  Call DP.Open(Session, DPOPEN_CREATE)

  CreatePlayer

  Form1.Timer1.Enabled = True
End Sub

Public Sub JoinSession()
  'here will be "discussed" which session you join
  Set Session = DPEnumSessions.GetItem(Form1.List2.ListIndex + 1)

  'check if one more player is allowed to join
  If Session.GetMaxPlayers < Session.GetCurrentPlayers Then End

  Session.SetGuidApplication MyGuid 'the chat/game uses the created GUID

  'open and join session
  DP.Open Session, DPOPEN_JOIN

  CreatePlayer

  Form1.Timer1.Enabled = True
End Sub

Public Sub CreatePlayer()
  'make attributes to the player/chatter...
  PlayerFriendly = InputBox("Nickname", "Create Player")
  PlayerFormal = InputBox("Real name", "Create Player")

  'now we create a player/chatter - the function returns an ID  
  'which we can use to identify us self... (later)
  PlayerIDNum = _
        DP.CreatePlayer(PlayerFriendly, PlayerFormal, 0, 0)
End Sub

Public Sub SendMessage()
  Dim Msg As DirectPlayMessage

  Set Msg = DP.CreateMessage

  'fill message
  Msg.WriteString (PlayerFriendly & ": " & Form1.Text1.Text)
  
  'send
  DP.SendEx PlayerIDNum, DPID_ALLPLAYERS, DPSEND_GUARANTEED, _
        Msg, 0, 0, 0

  Form1.List3.AddItem (PlayerFriendly & ": " & Form1.Text1.Text)
  Form1.Text1.Text = ("")
End Sub

Public Sub ReceiveMessage()
  Dim SourceIP As Long
  Dim TargetIP As Long
  Dim NumMessagesWaiting As Long
  Dim Msg As DirectPlayMessage
  
  NumMessagesWaiting = DP.GetMessageCount(PlayerIDNum) 'the number of waiting messages
  
  Do While NumMessagesWaiting > 0
    Set Msg = DP.Receive(SourceIP, TargetIP, DPRECEIVE_ALL)
    
    Form1.List3.AddItem Msg.ReadString
     
    'decrement number of waiting messages
    NumMessagesWaiting = NumMessagesWaiting - 1
  Loop

End Sub

我在开发过程中没有遇到任何问题。这里给出整个代码的一些部分。它不是很长,也不太难。

历史

  • 2005年3月23日:初始发布
© . All rights reserved.