如何在ios客户端连接socket.io 1.0? 我还使用以下代码使用iOS流类进行连接: -
- (void) initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(@"23.236.52.90"), 3000 , &readStream, &writeStream);
_inputStream = (__bridge_transfer NSInputStream *)readStream;
_outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[_inputStream setDelegate:self];
[_outputStream setDelegate:self];
[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_inputStream open];
[_outputStream open];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@"stream event %i", streamEvent);
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == _inputStream) {
uint8_t buffer[1024];
unsigned int len;
while ([_inputStream hasBytesAvailable]) {
len = [_inputStream read:buffer maxLength:1024];
if (len) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
NSLog(@"server said: %@", output);
[self messageReceived:output];
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;
default:
NSLog(@"Unknown event");
}
}
虽然我收到包含数据的缓冲区,但我总是将len视为0。我的服务器使用socket.io 1.1.0。
缓冲响应: -
(uint8_t [1024]) buffer = {
[0] = 'H'
[1] = '\xd7'
[2] = '\xff'
[3] = '\xbf'
[4] = '\x11'
[5] = '\x01'
[6] = '\xe2'
[7] = '\x8f'
[8] = '\x9c'
[9] = '\xde'
[10] = '\xc0'
[11] = '\x01'
[12] = '\xc8'
[13] = '\xeb'
[14] = '\xc0'
[15] = '\x01'
[16] = 'G'
[17] = 'O'
[18] = '\x84'
[19] = '\x06'
[20] = '\xbe'
[21] = '\x02'
[22] = '\xe2'
[23] = '\x8f'
[24] = '_'
[25] = '\0'
[26] = '\0'
[27] = '\0'
[28] = 'z'
[29] = '\0'
[30] = '\0'
[31] = '\0'
[32] = ' '
[33] = '\xe3'
[34] = '\xc0'
[35] = '\x01'
[36] = '\xce'
[37] = '\0'
[38] = '\xe2'
[39] = '\x8f'
[40] = '\xc8'
[41] = '+'
[42] = '\x02'
[43] = '\0'
[44] = 'X'
[45] = '\xf7'
[46] = '\xff'
[47] = '\xff'
[48] = '\0'
[49] = '\0'
[50] = '\0'
[51] = '\0'
[52] = '\xbe'
[53] = '\x02'
[54] = '\xe2'
[55] = '\x8f'
[56] = 'e'
[57] = '\0'
[58] = '\0'
[59] = '\0'
[60] = '\xc8'
[61] = '\xeb'
[62] = '\xc0'
[63] = '\x01'
[64] = '\0'
[65] = '\0'
[66] = '\0'
[67] = '\0'
[68] = '\xe2'
[69] = '\x98'
[70] = '\xe1'
[71] = '\x8f'
[72] = 'x'
[73] = '\xd8'
[74] = '\xff'
[75] = '\xbf'
[76] = '\xbc'
[77] = '\xfc'
[78] = '\xe3'
[79] = '\x8f'
[80] = '\x88'
[81] = '\xd7'
[82] = '\xff'
[83] = '\xbf'
[84] = '\x0e'
[85] = '\x99'
[86] = '\xe1'
[87] = '\x8f'
[88] = '\xbc'
[89] = '\xfc'
[90] = '\xe3'
[91] = '\x8f'
[92] = '0'
[93] = '\xa1'
[94] = '\xe3'
[95] = '\x8f'
[96] = ' '
[97] = '\xe3'
[98] = '\xc0'
[99] = '\x01'
[100] = '\xcc'
[101] = 'V'
[102] = '\xe4'
[103] = '\x8f'
[104] = '\x01'
[105] = '\0'
[106] = '\0'
[107] = '\0'
[108] = '\xc4'
[109] = '\x97'
[110] = '\xe1'
[111] = '\x8f'
[112] = '\xa8'
[113] = '\xd7'
[114] = '\xff'
[115] = '\xbf'
[116] = '\x90'
[117] = '\xc5'
[118] = '\xf2'
[119] = '\b'
[120] = '\0'
[121] = '\xd0'
[122] = '\xbf'
[123] = '\b'
[124] = '\x04'
[125] = '\0'
[126] = '\0'
[127] = '\0'
[128] = '\x03'
[129] = '\0'
[130] = '\0'
[131] = '\0'
[132] = '\0'
[133] = '\0'
[134] = '\0'
[135] = '\0'
[136] = '\0'
[137] = '\xbf'
[138] = '\xd1'
[139] = '\b'
[140] = '\0'
[141] = '\xbf'
[142] = '\xd1'
[143] = '\b'
[144] = '('
[145] = '\xd8'
[146] = '\xff'
[147] = '\xbf'
[148] = '\xc2'
[149] = '\a'
[150] = '\xe6'
[151] = '\x01'
[152] = '\x90'
[153] = '\xc5'
[154] = '\xf2'
[155] = '\b'
[156] = '@'
[157] = '\0'
[158] = '\0'
[159] = '\0'
[160] = '\x01'
[161] = '\0'
[162] = '\0'
[163] = '\0'
[164] = '\t'
[165] = 'a'
[166] = '\xe3'
[167] = '\b'
[168] = '\0'
[169] = '\xdf'
[170] = '\xaf'
[171] = '\b'
[172] = '\0'
[173] = '\xa0'
[174] = '\xaf'
[175] = '\b'
[176] = 'H'
[177] = '\xd8'
[178] = '\xff'
[179] = '\xbf'
[180] = 'v'
[181] = '\x03'
[182] = '\xe6'
[183] = '\x01'
[184] = '\xbb'
[185] = '\xff'
[186] = '\xe5'
[187] = '\x01'
[188] = '@'
[189] = '\0'
[190] = '\0'
[191] = '\0'
[192] = '\xd8'
[193] = '\xd7'
[194] = '\xff'
[195] = '\xbf'
[196] = 'x'
[197] = '\xd8'
[198] = '\xff'
[199] = '\xbf'
[200] = '\0'
[201] = '\xb5'
[202] = '\xd1'
[203] = '\b'
[204] = '\0'
[205] = '\n'
[206] = '\0'
[207] = '\0'
[208] = '\x18'
[209] = '\xd8'
[210] = '\xff'
[211] = '\xbf'
[212] = '\xc6'
[213] = '\x9c'
[214] = '\xe1'
[215] = '\x8f'
[216] = '\xbb'
[217] = '\xff'
[218] = '\xe5'
[219] = '\x01'
[220] = '\x1c'
[221] = '\0'
[222] = '\0'
[223] = '\0'
[224] = 'p'
[225] = '\xec'
[226] = '\xe3'
[227] = '\x8f'
[228] = '\0'
[229] = '\0'
[230] = '\0'
[231] = '\0'
[232] = '\0'
[233] = '\xd5'
[234] = '\xaf'
[235] = '\b'
[236] = '@'
[237] = '\0'
[238] = '\0'
[239] = '\0'
[240] = '\x02'
[241] = '\0'
[242] = '\0'
[243] = '\0'
[244] = '\xc2'
[245] = '\a'
[246] = '\xe6'
[247] = '\x01'
[248] = '\0'
[249] = '\xbf'
[250] = '\xd1'
[251] = '\b'
[252] = '\0'
[253] = '\xd0'
[254] = '\xbf'
[255] = '\b'
...
}