?? chatimpl.cs
字號:
namespace RemotingChat
{
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.TCP;
using System.Threading;
/// <summary>
/// Summary description for ChatImpl.
/// </summary>
//public delegate void Status(string strMessage);
public class ChatImpl : Chat
{
public enum myActions
{
SEND_MESSAGE=0,
SEND_QUIT=1,
SEND_LISTALL=2
}
private ChatServerImpl cs; //a reference to the server
private String name = null; //The user's name
private ServerTalker st; //Thread for handling message sending
private Form1 frm=null;
private string server=null;
public myActions myHandler;
public string Server
{
set
{
server=value;
}
}
public ChatImpl(Form1 frmClient)
{
this.frm=frmClient;
}
public void registerChatter()
{
TCPChannel chan = new TCPChannel();
ChannelServices.RegisterChannel(chan);
cs = (ChatServerImpl) Activator.GetObject(typeof(RemotingChat.ChatServerImpl), "tcp://" + server +":8085/SayHello");
if (cs== null)
System.Console.WriteLine("Could not locate server");
else
{
cs.register(this,name);
st = new ServerTalker(cs,name);
}
}
public string Name
{
set
{
name=value;
}
}
public bool handleEvent(myActions e,string strMessage)
{
bool flag=false;
switch (e)
{
case myActions.SEND_MESSAGE :
if(!st.addMessage(new Message(name,strMessage)))
{
frm.UpdateStatus("***Server Error***\n");
return false;
}
else
{
return true;
}
break;
case myActions.SEND_QUIT:
st.addMessage(new Message("***"+name,"Logged off. Bye"));
cs = null;
return true;
break;
case myActions.SEND_LISTALL:
this.getUserList();
break;
default:
break;
}
return false;
}
public void sendMessage(Line l)
{
if(!st.addMessage(new Message(name,l)))
frm.UpdateStatus("***Server Error***\n");
}
public void getUserList()
{
String[] users = null;
string strUser=null;
try
{
users = cs.listChatters();
}
catch (Exception e)
{
users = new String[1];
users[0] = "***Error";
strUser=users[0];
}
for(int i = 0; i < users.Length; i++)
strUser +="***"+users[i]+"\n";
frm.UpdateStatus(strUser);
}
public void chatNotify(Message m)
{
Monitor.Enter(this);
if(m.getMessage()!=null)
frm.UpdateStatus(m.getSender() + ": " +m.getMessage()+"\n");
Monitor.Exit(this);
}
public String getName(){
return name;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -