我开始学习 C# 并且无法弄清楚如何从接口方法中调用基类方法。在这种特殊情况下,我需要以某种方式sendData
从方法内部调用onRecieve
。
WebSocketBase.cs
using WebSocketSharp;
using System;
using System.Collections.Generic;
using System.Threading;
class WebSocketBase
{
private WebSocket webSocketClient = null;
private WebSocketService webService = null;
private string url = null;
public WebSocketBase(string url, WebSocketService webService)
{
this.webService = webService;
this.url = url;
}
public void start()
{
webSocketClient = new WebSocket(url);
webSocketClient.OnError += new EventHandler<WebSocketSharp.ErrorEventArgs>(webSocketClient_Error);
webSocketClient.OnOpen += new EventHandler(webSocketClient_Opened);
webSocketClient.OnClose += new EventHandler<WebSocketSharp.CloseEventArgs>(webSocketClient_Closed);
webSocketClient.OnMessage += new EventHandler<MessageEventArgs>(webSocketClient_MessageReceived);
webSocketClient.ConnectAsync();
}
private void webSocketClient_MessageReceived(object sender, MessageEventArgs e)
{
webService.onReceive(e.Data);
}
public void send(string channel)
{
webSocketClient.Send(channel);
}
public void sendData(string data)
{
send("data");
}
}
interface WebSocketService
{
void onReceive(String msg);
}
逻辑.cs
class Logic : WebSocketService
{
public void onReceive(string msg)
{
Console.WriteLine("Received message")
}
}