linux写聊天程序,轻易实现基于linux或win运行的聊天服务端程序
對于不了解網(wǎng)絡(luò)編程的開發(fā)人員來說,編寫一個良好的服務(wù)端通訊程序是一件比較麻煩的事情.然而通過EC這個免費組件你可以非常簡單地構(gòu)建一個基于linux或win部署運行的網(wǎng)絡(luò)服務(wù)程序.這種便利性完全得益于mono這些年來的不停發(fā)展.下面介紹通過EC這個組件如何通過短短十來分鐘的時候內(nèi)就能實現(xiàn)一個聊天室通訊服務(wù)程序.
在實現(xiàn)一個網(wǎng)絡(luò)通訊程序的時候需要定義一個通訊協(xié)議,但EC已經(jīng)集成了基礎(chǔ)的協(xié)議功能,只需要根據(jù)交互的數(shù)據(jù)定義消息類型即可(EC提供兩種序列化對象描述分別是protobuf和msgpack).
消息定義
針對簡單的聊到室只需要定義登進,登出和發(fā)言這幾個消息如下:
[MessageID(0x0001)]
[ProtoContract]
public class Login
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public string From { get; set; }
}
[MessageID(0x0003)]
[ProtoContract]
public class Signout
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public string From { get; set; }
}
[MessageID(0x0002)]
[ProtoContract]
public class Say
{
[ProtoMember(1)]
public string Content { get; set; }
[ProtoMember(3)]
public string From { get; set; }
[ProtoMember(2)]
public string Name { get; set; }
}
服務(wù)端
消息定義完成那用EC來制定一個聊天轉(zhuǎn)發(fā)的服務(wù)端來說則是件非常簡單的事情,只需要十來行代碼就可以構(gòu)建聊天和服務(wù)啟動等相關(guān)功能.
[EC.Controller]
public class Program
{
static void Main(string[] args)
{
EC.ECServer.Open();
System.Threading.Thread.Sleep(-1);
}
public void OnLogin(EC.ISession session, Chat.Login e)
{
session.Channel.Name = e.Name;
e.From = session.Channel.EndPoint.ToString();
foreach (Beetle.Express.IChannel other in session.Application.Server.GetOnlines())
{
if (other != session.Channel)
session.Application.Server.Send(e, other);
}
}
public void OnSay(EC.ISession session, Chat.Say e)
{
e.Name = session.Channel.Name;
e.From = session.Channel.EndPoint.ToString();
foreach (Beetle.Express.IChannel other in session.Application.Server.GetOnlines())
{
if (other != session.Channel)
session.Application.Server.Send(e, other);
}
}
}
以上一個簡單的聊取室的登進和聊天的功能,不過還有一個需要我們?nèi)ヌ幚淼木褪钱斢脩魯嚅_后如果反映給其他用戶.在EC中監(jiān)控連接斷開的過程需要通過一個AppModel來監(jiān)控,發(fā)布有連接斷開了則向其他連接發(fā)送登出信息,代碼如下:
public class AppModel : EC.IAppModel
{
public void Init(EC.IApplication application)
{
application.Disconnected += (o, e) =>
{
Beetle.Express.IChannel channel = e.Session.Channel;
Chat.Signout msg = new Signout();
msg.Name = channel.Name;
msg.From = channel.EndPoint.ToString();
foreach (Beetle.Express.IChannel other in application.Server.GetOnlines())
{
if (other != channel)
application.Server.Send(msg, other);
}
};
}
public string Name
{
get { return "AppModel"; }
}
public string Command(string cmd)
{
throw new NotImplementedException();
}
}
EC提供一個IAppModel的自定義功能,通過AppModel可以監(jiān)控用戶會話,和處理全局消息的能力;在以后的文章再詳細介紹.
客戶端
EC同樣提供便利的Client功能對象,你只需要定義簡單的代碼就可以向?qū)?yīng)的服務(wù)端發(fā)送和接收相應(yīng)的消息來處理.
EC.ProtoClient mClient = new EC.ProtoClient("127.0.0.1");
mClient.Receive = (o, p) => {
if (p.Message is Say)
{
Invoke(new Action(OnSay), p.Message);
}
else if (p.Message is Login)
{
Invoke(new Action(OnLogin), p.Message);
}
else if (p.Message is Signout)
{
Invoke(new Action(OnSignout), p.Message);
}
};
mClient.Send(new Say{ Content=t"你好"});
借助于Xamarin我們還可以同樣的方式把功能移植到不同平臺下運行如android,ios等
private IServiceChannel mClient = new ServiceChannel("10.0.2.2",10034);
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
ServiceChannel.Register (typeof(MainActivity).Assembly);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
EditText name = FindViewById (Resource.Id.txtname);
EditText say = FindViewById (Resource.Id.txtsay);
TextView content = FindViewById (Resource.Id.txtContent);
mClient.Receive = (o, p) => {
content.Post(delegate {
content.Append(p.Message.ToString());
});
};
FindViewById (Resource.Id.btnlogin).Click += delegate {
Login login = new Login();
login.Name = name.Text;
mClient.Send(login);
};
FindViewById (Resource.Id.btnsay).Click += delegate {
Say s = new Say{ Content=say.Text};
mClient.Send(s);
};
// Get our button from the layout resource,
// and attach an event to it
}
這樣一個多平臺的基礎(chǔ)聊天功能就完成了
總結(jié)
以上是生活随笔為你收集整理的linux写聊天程序,轻易实现基于linux或win运行的聊天服务端程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 9i sql_id,Ora
- 下一篇: linux crontab日志,cron