さらに説明や詳細が必要な場合は、コメントしてください..
リクエストに基づいてクライアントにメッセージを送信するローカル python サーバーをコンピューターで実行しています。
私は3つのクラスを持つMACクライアントアプリケーションを持っています -
Chatlogic クラス - このクラスは、接続を初期化し、サーバーからメッセージを送受信します。
ログイン クラス - これは、アプリケーションへのユーザーのログインを維持します。このクラスには、Chatlogic クラスのインスタンスがあり、この [chatLogicObject sendmessage: something ]のように、chatlogic クラスのオブジェクトを介してメッセージを送信できます。
私の問題はこれです=受信すると、LoginClassではなくchatLogicクラスインスタンスに来るので、ログインクラスに-(void)messageReceivedというメソッドがあり、chatLogicクラスの同じメソッドをオーバーライドする必要があります(しかし、これはしません仕事)。Loginclass でメソッドを受け取るにはどうすればよいですか?
混乱を避けるために、chatlogic クラスを追加しました
#import <Foundation/Foundation.h>
@interface chatLogic : NSObject <NSStreamDelegate>
@property (copy)NSOutputStream *outputStream;
@property (copy)NSInputStream *inputStream;
@property (copy)NSString *streamStatus;
-(void)initNetworkCommunications;
-(void)sendMessage:(id)sender;
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent;
-(void)messageReceived:(NSString*)message; //i want this method to be used in some other classes
@end
実装ファイルは以下の通り
「chatLogic.h」をインポート
@実装チャットロジック
@synthesize 入力ストリーム ; @synthesize outputStream ; @synthesize streamStatus;
-(id)init{
if (self == [super init]) {
}
return self;
}
-(void)initNetworkCommunications{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.22", 80, &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];
}
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent{
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
streamStatus = [[NSString alloc]initWithFormat:@"Stream opened"];
break;
case NSStreamEventHasBytesAvailable:
if (aStream == inputStream) {
uint8_t buffer[1024];
int len;
while ([inputStream hasBytesAvailable]) {
len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc]initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
NSLog(@"server said: %@",output);
[self messageReceived:output];
}
}
}
}
case NSStreamEventErrorOccurred:
{
NSLog(@"cannot connect to the host!");
break;
}
case NSStreamEventEndEncountered:
{
break;
}
default:
{
NSLog(@"unknown event!!");
}
}
}
-(void)sendMessage:(id)送信者{
}
-(void)messageReceived:(NSString*)メッセージ{
NSLog(@"the received message in chat logic class");
}
-(void)dealloc{
[inputStream close];
[outputStream close];
[inputStream release];
[outputStream release];
[super dealloc];
}
@終わり