.net聊天应用程序问题 [英] .net Chat app problem
本文介绍了.net聊天应用程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用.NET Remoting编写聊天应用程序。问题是我想使用Invoke在客户端编辑一个表单(addMsg),但我不能调用它,因为我只能实例化超类(在clientObject构造函数中)而不是子类,因为它对我来说不可见。我无法很好地解释这个问题,但它在评论中的代码中是明确的。
客户代码:
Im coding a Chat App using .NET Remoting. The problem is that i want to use Invoke to edit a form (addMsg) in the client but i cant call it because i can only instantiate the superclass (in clientObject constructor) and not the subclass because its not visible for me. I can't explain the problem very well but its explicit in the code in the comments.
Client code:
namespace DADChat {
public partial class Form1 : Form
{
private MyRemoteObject objetoServidor = null;
private clientObject objetoCliente = null;
public Form1()
{
InitializeComponent();
}
private void sendButton_Click(object sender, EventArgs e)
{
string mensagem = messageBox.Text;
string nick = nickBox.Text;
objetoServidor.enviaMensagem(nick, mensagem);
}
public void addMsg(string m) //adiciona mensagem à conversa
{
textBox4.Text += "\r\n" + m;
}
private void connectButton_Click(object sender, EventArgs e)
{
string url = "tcp://localhost:" + portBox.Text + "/objectClientName";
int port = Int32.Parse(portBox.Text);
string nick = nickBox.Text;
TcpChannel channel = new TcpChannel(port);
ChannelServices.RegisterChannel(channel, true);
objetoCliente = new clientObject(this); /* i use this to pass the reference of this object so i can edit the form in the remote class */
RemotingServices.Marshal(objetoCliente, "objectClientName", typeof(clientObject));
objetoServidor = (MyRemoteObject)Activator.GetObject(typeof(MyRemoteObject), "tcp://localhost:8096/MyRemoteObjectName");
NovoCliente cliente = new NovoCliente();
cliente.Nome = nick;
objetoServidor.adicionarCliente(cliente.Nome, url, port);
objetoServidor.printListaClientes();
}
}
}
The Remote class: namespace ChatObjects { public delegate void delChat(string m);
public class MyRemoteObject : MarshalByRefObject
{
private List<novocliente> listaClientes = new List<novocliente>();
public event delChat serverParaCliente;
public string metodoOla()
{
return "Ola!";
}
public void adicionarCliente(string nome, string url,int port)
{
if (nome != null)
{
NovoCliente cliente = new NovoCliente();
cliente.Nome = nome;
cliente.Porto = port;
cliente.URL = url;
listaClientes.Add(cliente);
}
}
public void printListaClientes()
{
for (int i = 0; i < listaClientes.Count; i++)
Console.WriteLine("Cliente: " + listaClientes[i].Nome + " e a sua localização é " + listaClientes[i].Porto + "\n");
}
public void enviaMensagem(string nickname, string mensagem) //método responsável por receber a mensagem do utilizador e envia para o chat
{
string mensagemFinal = nickname + ": " + mensagem;
foreach (NovoCliente cliente in listaClientes)
{
clientObject obj = (clientObject)Activator.GetObject(typeof(clientObject), cliente.URL);
obj.recebeMsg(mensagemFinal);
}
}
}
public class clientObject : MarshalByRefObject
{
public Form f; /* the problem resides here because to call addMsg it should Form1 f and not Form */
public clientObject(Form f)
{
this.f = f;
}
public void recebeMsg(string msg)
{
f.Invoke(new delChat(f.addMsg), new object[] { msg }); /* cant call addMsg */
}
}
public class NovoCliente
{
private string nome;
private int porto;
private string url;
public string Nome
{
get
{
return nome;
}
set
{
nome = value;
}
}
public string URL
{
get
{
return url;
}
set
{
url = value;
}
}
public int Porto
{
get
{
return porto;
}
set
{
porto = value;
}
}
}
}
提前感谢您的帮助
Thank you for the help in advance
推荐答案
我不确定您的聊天代码是如何工作的,但是我可以建议使用addMsg()方法的接口,并将您的clientObject类修改如下。
I am not sure how your chat code works, however I can suggest to use an interface with addMsg() method and have your clientObject class modified like below.
public interface MyInterface
{
void addMsg(string msg);
}
public class MyRemoteObject : MarshalByRefObject, MyInterface
{
//Your exisitng code
}
public class clientObject : MarshalByRefObject
{
public Form f;
public clientObject(Form f)
{
this.f = f;
}
public void recebeMsg(string msg)
{
MyInterface obj = (MyInterface) f;
f.Invoke(new delChat(obj.addMsg), new object[] { msg });
}
}
这篇关于.net聊天应用程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文