那么在这四个标记中,[RemoteIP]和[LocateIP]之间是远端IP地址的数据,在[LocateIP]和[SendDate]之间是本地IP地址的数据,在[SendDate]和[SendData]之间是发送时的本地时间,在[SendData]一直到最后是聊天的内容。
封装是将已经完成的方法和属性、事件一起,包裹在一个密封的容器里,留出一个入口可以和外面联络。为此我们希望将我们的聊天作为一种方式封装起来。那么对于聊天来说,我们需要一些什么接口呢?南海把所需的一些归纳成如下几点:
| 属性: |
| persons |
集合 |
朋友的集合 |
| person |
单项 |
|
| 方法: |
| connect |
|
和服务器连接 |
| send |
|
象朋友发送消息 |
| 事件: |
| evtPersonData |
|
从服务器得到资料 |
| evtReceiveData |
|
朋友发送消息过来 |
为此,我们将接口做成如下的部分:
clsChat:
Option Explicit
Private mvarPerson As clsPersons ’local copy
’To fire this event, use RaiseEvent with the following syntax:
’RaiseEvent evtListenData[(arg1, arg2, ... , argn)]
Public Event evtListenData(ByVal id As String, status As Boolean)
’To fire this event, use RaiseEvent with the following syntax:
’RaiseEvent evtReceiveData[(arg1, arg2, ... , argn)]
Public Event evtReceiveData(id As String, sData As String)
’local variable(s) to hold property value(s)
Private mvarSendSock As Winsock ’local copy
Private mvarListenSock As Winsock ’local copy
Public Property Set ListenSock(ByVal vData As Winsock)
’used when assigning an Object to the property, on the left side of a Set statement.
’Syntax: Set x.ListenSock = Form1
Set mvarListenSock = vData
End Property
Public Property Set SendSock(ByVal vData As Winsock)
’used when assigning an Object to the property, on the left side of a Set statement.
’Syntax: Set x.SendSock = Form1
Set mvarSendSock = vData
End Property
Public Sub send(sData As String)
End Sub
Public Sub connect()
End Sub
Public Property Set Person(ByVal vData As clsPersons)
Set mvarPerson = vData
End Property
Public Property Get Person() As clsPersons
Set Person = mvarPerson
End Property
Private Sub Class_Initialize()
Set mvarPerson = New clsPersons
End Sub
Private Sub Class_Terminate()
Set mvarPerson = Nothing
End Sub
clsPersons:
Option Explicit
’local variable to hold collection
Private mCol As Collection
Public Function Add(Key As String, Optional sKey As String) As clsPerson
’create a new object
Dim objNewMember As clsPerson
Set objNewMember = New clsPerson
’set the properties passed into the method
objNewMember.Key = Key
If Len(sKey) = 0 Then
mCol.Add objNewMember
Else
mCol.Add objNewMember, sKey
End If
’return the object created
Set Add = objNewMember
Set objNewMember = Nothing
End Function
Public Property Get Item(vntIndexKey As Variant) As clsPerson
’used when referencing an element in the collection
’vntIndexKey contains either the Index or Key to the collection,
’this is why it is declared as a Variant
’Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
Set Item = mCol(vntIndexKey)
End Property
Public Property Get Count() As Long
’used when retrieving the number of elements in the
’collection. Syntax: Debug.Print x.Count
Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
’used when removing an element from the collection
’vntIndexKey contains either the Index or Key, which is why
’it is declared as a Variant
’Syntax: x.Remove(xyz)
mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
’this property allows you to enumerate
’this collection with the For...Each syntax
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
’creates the collection when this class is created
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
’destroys collection when this class is terminated
Set mCol = Nothing
End Sub
clsPerson:
Option Explicit
Public Key As String
’local variable(s) to hold property value(s)
Private mvarID As String ’local copy
Private mvarthereIP As String ’local copy
Private mvarnowStatus As Boolean ’local copy
Private mvarChatDatas As String ’local copy
Private mvartopic As String ’local copy
Private mvarthsName As String ’local copy
Private Type ChatRecord
thisDate As String
thisNick As String
thisData As String
thisshow As Boolean
End Type
Public Property Let thsName(ByVal vData As String)
’used when assigning a value to the property, on the left side of an assignment.
’Syntax: X.thsName = 5
mvarthsName = vData
End Property
Public Property Get thsName() As String
’used when retrieving value of a property, on the right side of an assignment.
’Syntax: Debug.Print X.thsName
Set thsName = mvarthsName
End Property
Public Property Let topic(ByVal vData As String)
’used when assigning a value to the property, on the left side of an assignment.
’Syntax: X.topic = 5
mvartopic = vData
End Property
Public Property Get topic() As String
’used when retrieving value of a property, on the right side of an assignment.
’Syntax: Debug.Print X.topic
Set topic = mvartopic
End Property
Public Property Let ChatDatas(ByVal vData As String)
’used when assigning a value to the property, on the left side of an assignment.
’Syntax: X.ChatDatas = 5
mvarChatDatas = vData
End Property
Public Property Get ChatDatas() As String
’used when retrieving value of a property, on the right side of an assignment.
’Syntax: Debug.Print X.ChatDatas
ChatDatas = mvarChatDatas
End Property
Public Property Let nowStatus(ByVal vData As Boolean)
’used when assigning a value to the property, on the left side of an assignment.
’Syntax: X.nowStatus = 5
mvarnowStatus = vData
End Property
Public Property Get nowStatus() As Boolean
’used when retrieving value of a property, on the right side of an assignment.
’Syntax: Debug.Print X.nowStatus
Set nowStatus = mvarnowStatus
End Property
Public Property Let thereIP(ByVal vData As String)
’used when assigning a value to the property, on the left side of an assignment.
’Syntax: X.thereIP = 5
mvarthereIP = vData
End Property
Public Property Get thereIP() As String
’used when retrieving value of a property, on the right side of an assignment.
’Syntax: Debug.Print X.thereIP
Set thereIP = mvarthereIP
End Property
Public Property Let id(ByVal vData As String)
’used when assigning a value to the property, on the left side of an assignment.
’Syntax: X.ID = 5
mvarID = vData
End Property
Public Property Get id() As String
’used when retrieving value of a property, on the right side of an assignment.
’Syntax: Debug.Print X.ID
Set id = mvarID
End Property
因为南海没有搞到一个好点的服务器,所以没有办法给大家一个好点的测试[平台,接下来的工作是相当的枯燥的,而现在该交代的都交代了,所以不如大家将南海前面所讲的好好归纳一下,看看你是否能够做一个出来呢?还有对做组件不懂的朋友,请你将你的问题告诉南海。今天这个算是给大家的作业吧:)我会在两天后将这些详细内容告诉大家,对于想知道答案的朋友,你可以期待南海的详细的讲解。而对于想挑战自己的朋友,你只有两天时间呀。所以,赶快……(注:公布时间是9月25号)