iOS 开发,基于GCDAsyncSocket的即时通讯,是怎样在两个用户间实现的??大概说下过程
展开全部
I think you're slightly misunderstanding the tag concept. The read operations aren't saying "Read data that has been tagged as 2". They are saying "Read the next data off the wire, and tag it as 2 for future reference."
The tag is never sent over the wire - the server didn't tag the data and send it to the client to read. It's a completely optional concept only used to distinguish local operations from each other. In other words, The data being read has no tag. The tag is something you assign to the read operation, so you can identify it later once it's complete.
For example, say you're reading data as a series of headers and payloads. You could use the tag to distinguish a header read from a payload read:
const NSInteger kHeaderTag = 1;
const NSInteger kPayloadTag = 2;
// Assume you know to expect a header, so tag the read operation as such.
[self readDataWithTimeout:-1 tag:kHeaderTag];
// Next assume you know to expect a payload, so tag the read operation as such.
[self readDataWithTimeout:-1 tag:kPayloadTag];
Then you can identify it later...
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
if (tag == kHeaderTag)
{
// Handle header
}
else if (tag == kPayloadTag)
{
// Handle payload
}
}
The tag is never sent over the wire - the server didn't tag the data and send it to the client to read. It's a completely optional concept only used to distinguish local operations from each other. In other words, The data being read has no tag. The tag is something you assign to the read operation, so you can identify it later once it's complete.
For example, say you're reading data as a series of headers and payloads. You could use the tag to distinguish a header read from a payload read:
const NSInteger kHeaderTag = 1;
const NSInteger kPayloadTag = 2;
// Assume you know to expect a header, so tag the read operation as such.
[self readDataWithTimeout:-1 tag:kHeaderTag];
// Next assume you know to expect a payload, so tag the read operation as such.
[self readDataWithTimeout:-1 tag:kPayloadTag];
Then you can identify it later...
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
if (tag == kHeaderTag)
{
// Handle header
}
else if (tag == kPayloadTag)
{
// Handle payload
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询