#import "AppDelegate.h"
@implementation AppDelegate
@synthesize inputStream;
@synthesize outputStream;
@synthesize textField;
@synthesize window;
@synthesize aText;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self initNetworkCommunication];
}
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"tec.skotos.net", 6730, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
NSString *response = [NSString stringWithFormat:@"/\\/connect: n/a!!n/a"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
NSString *end = @"\n";
NSData *endData = [[NSData alloc] initWithData:[end dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
[outputStream write:[endData bytes] maxLength:[endData length]];
}
- (IBAction)sendMessage:(id)sender {
NSString *response = [textField stringValue ];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
NSString *end = @"\n";
NSData *endData = [[NSData alloc] initWithData:[end dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
[outputStream write:[endData bytes] maxLength:[endData length]];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"Has space");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[900000];
long len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
NSLog(@"Has Bytes");
if (nil != output) {
NSLog(@"%@", output);
[window setString:output];
}
}
}
}
break;
case NSStreamEventEndEncountered:
NSLog(@"Error.");
break;
}
}
@end
所以,我正在尋找一個雄辯的方式來改變[窗口setString:輸出]方法。這是我爲了玩MUD(基於文本的遊戲)而編寫的程序的.m文件。 inputstream表示一個連接到發送我的客戶信息(字節)的遊戲服務器。每次hasBytesAvailable事件發生時,字節都會轉換爲字符串。這些字節然後顯示在窗口的文本字段上。我遇到的問題是,每次發生事件時,它都會取代最初在屏幕上顯示的內容。我試圖創建一個佔位符字符串,它保存輸出數據,然後附加到該字符串中顯示。它似乎沒有按照我想要的方式工作。我希望能夠不斷從流中獲取數據,並在整個程序中逐行顯示。我試過NSArray和NSMutableString ...我似乎無法弄清楚如何創建全局字符串變量,我可以每次發生hasBytesAvailable事件時追加。謝謝大家的閱讀,我希望你能幫助。如何正確顯示到NSTextView? NSString/NSInputStream/NSTextView
那麼,如果是這種情況,那麼創建singleton類insode聲明你的mutablestring,只需追加inisde,並設置到屏幕 –
完美的工作!不好意思,因爲我現在只學了幾個禮拜。 –
我從套接字服務器接收到的字符串是用HTML標記的,創建一個類去除HTML標籤併爲實際標記設置功能(如等)的好方法是... –