Ice topic: implementing simple chat rooms (1)

Source: Internet
Author: User

Objective: To implement a simple chat room. The chat room in this article only broadcasts messages sent by one client to other registered clients to demonstrate the ice multicast function. In the future series of articles, we will gradually improve this example to make it a practical chat room software available.

Slice definition:

 

Module chatspacedef {// callback function interface, that is, the method proxy signature interface callback {void getinput (string content) received by the client to the server );}; // online list dictionary <string, callback *> cachemap; // chat room function list interface chatspace {bool register (string name); // register void setinput (string content ); // send the message void unregister (); // exit void setupcallback (callback * CP); // set callback };}; run the following command:
slice2cs --output-dir generated *.ice
This command generates the chatspacedef. CS file of the application skeleton used by both the client and server. To understand all the information about the class of the file,
Please refer to the ice user manual.
Define the server
For the server, you can implement the chatspacedisp _ abstract base class. In other words, you must implement:
Bool register (string name); // register void setinput (string content); // send the message void unregister (); // exit void setupcallback (callback * CP ); // set callback

These four methods. The Code is as follows:
Using system; using system. collections. generic; using system. text; using chatspacedef; namespace chatspace {public sealed class chatspacei: chatspacedisp _ {public override bool register (string name, ice. current _) {console. writeline ("verify that {0} is registered! ", Name); return! M_cache_map.contains (name);} public override void setinput (string content, ice. Current __){
// Broadcast message to others string user_name = Current __. CTX ["user_name"]; foreach (string key in m_cache_map.keys) {If (Key = user_name) {continue;} m_cache_map [Key]. getinput (content, current __. CTX) ;}} public override void unregister (ice. current _) {If (current __. CTX ["user_name"]! = NULL) {m_cache_map.remove (current __. CTX ["user_name"]); console. writeline ("logout {0} succeeded", current __. CTX ["user_name"]) ;}} public override void setupcallback (callbackprx CP, ice. current _) {If (current __! = NULL & Current _. CTX ["user_name"]! = NULL) {string user_name = Current __. CTX ["user_name"]; console. writeline ("the context is user_name = {0}", user_name); m_cache_map.add (user_name, CP);} else {Throw new exception ("context error! ") ;}} Cachemap m_cache_map = new cachemap ();}}
The above code is very easy to understand. It must be explained that ice. Current indicates the context information carried by the caller. This is the internal mechanism of ice. You can
The context object carries other call-related information. Note that it is not a method call parameter. It is a network connection between the client and server.
It is a carrier for transmitting information in the network environment.

Configure config. server on the server:
Callback.Server.Endpoints=tcp -p 12345:udp -p 12345## Warn about connection exceptions#Ice.Warn.Connections=1## Network Tracing## 0 = no network tracing# 1 = trace connection establishment and closure# 2 = like 1, but more detailed# 3 = like 2, but also trace data transfer##Ice.Trace.Network=1## Protocol Tracing## 0 = no protocol tracing# 1 = trace protocol messages##Ice.Trace.Protocol=1## Security Tracing## 0 = no security tracing# 1 = trace messages##IceSSL.Trace.Security=1
The main program on the server side is very simple server. CS:
using System;using System.Collections.Generic;using System.Text;namespace ChatSpace{    public class Server : Ice.Application    {        public override int run(string[] args)        {            Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Callback.Server");                       adapter.add(new ChatSpaceI(), communicator().stringToIdentity("callback"));            adapter.activate();            communicator().waitForShutdown();            return 0;        }        public static void Main(string[] args)        {            Server app = new Server();            int status = app.main(args, "config.server");            if (status != 0)            {                System.Environment.Exit(status);            }        }    }}
 
 

 

Define the client

For the client, you only need to implement the callback method to call callbacki. CS.

Using system; using system. collections. generic; using system. text; using chatspacedef; namespace client {public sealed class callbacki: callbackdisp _ {public override void getinput (string content, ice. current _) {system. console. writeline ("The message passed by the server: {0} say: {1}", current __. CTX ["user_name"], content );}}}
Client main program client. CS:
 
Using system; using system. collections. generic; using system. text; using chatspacedef; namespace client {class client: Ice. application {static void main (string [] ARGs) {client APP = new client (); int status = app. main (ARGs, "config. client "); If (status! = 0) {system. environment. exit (Status) ;}} public override int run (string [] ARGs) {chatspaceprx spaceprx = chatspaceprxhelper. checkedcast (communicator (). propertytoproxy ("callback. callbackserver "); If (spaceprx = NULL) {system. console. writeline ("the network configuration is invalid! "); Return 1;} system. console. writeline ("enter username: \ r \ n"); string user_name = console. readline (); If (spaceprx. register (user_name) = false) {system. console. writeline ("this user name has been registered! "); Return 1;} ice. objectadapter adapter = communicator (). createobjectadapter ("callback. client "); adapter. add (New callbacki (), communicator (). stringtoidentity ("callbackreceiver"); adapter. activate (); callbackprx = callbackprxhelper. uncheckedcast (adapter. createproxy (communicator (). stringtoidentity ("callbackreceiver"); ice. context CTX = new ice. context (); CTX. add ("user_name", user_nam E); spaceprx. setupcallback (callbackprx, CTX); system. console. writeline ("Enter the chat content: \ r \ n"); string content = ""; while (content! = "X") {content = system. Console. Readline (); spaceprx. setinput (content, CTX);} spaceprx. unregister (CTX); Return 0 ;}}}
Client configuration file:

Callback. callbackserver = callback: TCP-P 12345: UDP-P 12345callback. client. endpoints = TCP: UDP # warn about connection exceptions # ice. warn. connections = 1 # network tracing #0 = No network tracing #1 = trace connection establishment and closure #2 = like 1, but more detailed #3 = like 2, but also trace data transfer # ice. trace. network = 1 ## protocol tracing #0 = No protocol tracing #1 = trace protocol m Essages # ice. trace. protocol = 1 # Security tracing #0 = No security tracing #1 = trace messages # icessl. trace. security = 1 now, a multicast chat room is complete.

Compile and run:

 
Ice, SQLite, jasperreport ...... Open-source technology discussion group: 25935569. Welcome to our friends who are interested in the application of open-source technology for progress.
Project download: icechatspace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.