Magic go language (chat room development) and go language chat room Development
[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
This is a blog about chat room development. The original Article address is from here. This article is very representative. I don't want to talk about the function content in the code, but I just want to explain the implementation of it. You can read the code either here or go to the blog to continue reading it.
Package mainimport ("fmt" "OS" "net ") //////////////////////////////////////// /// // error check ////////////////// //////////////////////////////////////// func checkError (err error, info string) (res bool) {if (err! = Nil) {fmt. println (info + "" + err. error ()) return false} return true }////////////////////////////////// //// // the server side receives the data thread // parameter: // data connection conn // communication channel messages ////////////////////////////// /// // func Handler (conn net. conn, messages chan string) {fmt. println ("connection is connected from... ", conn. remoteAddr (). string () buf: = make ([] byte, 1024) for {lenght, err: = conn. read (Buf) if (checkError (err, "Connection") = false) {conn. close () break} if lenght> 0 {buf [lenght] = 0} // fmt. println ("Rec [", conn. remoteAddr (). string (), "] Say:", string (buf [0: lenght]) reciveStr: = string (buf [0: lenght]) messages <-reciveStr }}////////////////////////////////// //// // the thread that the server sends data // parameters // connection dictionary conns // data channel messages ////////////////////////////////// /// // func echoHa Ndler (conns * map [string] net. conn, messages chan string) {for {msg: = <-messagesfmt. println (msg) for key, value: = range * conns {fmt. println ("connection is connected from... ", key) _, err: = value. write ([] byte (msg) if (err! = Nil) {fmt. println (err. error () delete (* conns, key )}}}} //////////////////////////////////////// //// // start the server // parameter // port /////////// //////////////////////////////////////// ///// func StartServer (port string) {service: = ":" + port // strconv. itoa (port); tcpAddr, err: = net. resolveTCPAddr ("tcp4", service) checkError (err, "ResolveTCPAddr") l, err: = net. listenTCP ("tcp", tcpAddr) checkError (err, "ListenT CP ") conns: = make (map [string] net. conn) messages: = make (chan string, 10) // start the server broadcast thread go echoHandler (& conns, messages) for {fmt. println ("Listening... ") conn, err: = l. accept () checkError (err, "Accept") fmt. println ("Accepting... ") conns [conn. remoteAddr (). string ()] = conn // start a new thread go Handler (conn, messages )}} //////////////////////////////////////// ///// // The sending thread of the client // parameter // send connection conn ///////// ///////////////// /// // Func chatSend (conn net. conn) {var input stringusername: = conn. localAddr (). string () for {fmt. scanln (& input) if input = "/quit" {fmt. println ("ByeBye .. ") conn. close () OS. exit (0);} lens, err: = conn. write ([] byte (username + "Say:" + input) fmt. println (lens) if (err! = Nil) {fmt. println (err. error () conn. close () break }}}//////////////////////////////////// //// // client startup function // parameters // remote IP address and port tcpaddr /// //////////////////////////////////////// //////////// func StartClient (tcpaddr string) {tcpAddr, err: = net. resolveTCPAddr ("tcp4", tcpaddr) checkError (err, "ResolveTCPAddr") conn, err: = net. dialTCP ("tcp", nil, tcpAddr) checkError (err, "DialTCP") // start the client sending thread go ch AtSend (conn) // start the client training round buf: = make ([] byte, 1024) for {lenght, err: = conn. read (buf) if (checkError (err, "Connection") = false) {conn. close () fmt. println ("Server is dead... byeBye ") OS. exit (0)} fmt. println (string (buf [0: lenght])} //////////////////////////////////////// ////////////////// main program //// parameter description: // start the server: Chat server [port] eg: Chat Server 9090 // start the client: Chat client [Server Ip Addr]: [server Port] eg: Chat cli Ent 192.168.0.74: 9090 /////////////////////////////////////// //// // func main () {if len (OS. args )! = 3 {fmt. println ("Wrong pare") OS. exit (0)} if OS. args [1] = "server" & len (OS. args) = 3 {StartServer (OS. args [2])} if OS. args [1] = "client" & len (OS. args) = 3 {StartClient (OS. args [2])}
The main knowledge points are as follows:
(1) The Code contains the content of the server and client. If it is a server, enter./chat server 9090 directly. The client is also very simple. Enter./chat client: 9090;
(2) if it is a client, it actually includes two parts, one is the chatSend function, which accepts user input, and the other is connect to the server to accept relevant information;
(3) if it is a server, it is a little more complicated and consists of three parts. The first part is to constantly accept each client; the second is to create a Handler function for each client to accept the information sent from the client; the third is the echoHandler function, its function is to broadcast the information received from a user to all other clients, which is so simple.
It is highly expensive to create a system with voice and video similar to a chat room. Is the network speed affected and the server bandwidth important?
1. Video voice control (including acquisition, compression, transmission, display, video recording, playback, photo taking, and other functions, with good results)
It can be easily used for instant messaging, chat rooms, video conferences, remote teaching, and secondary development of video monitoring.
2. Simple and practical professional financial management system source code:
Based on the customer's requirements and years of experience, the advantages of multiple financial software, such as yonyou and kingdisk, the c/s structure, standalone version,
Online Multi-user, SQL database, set of books management flow program operation, simple, easy to use, lan, the Internet is very suitable
3. Simple and practical financial invoicing system source code:
Mature and stable financial invoicing software, c/s structure, online version, SQL database, and set of Books Management
Stream operation, simple, easy to use, lan, remote chain stores are very suitable.
4. Well-known video game source code and website source code
Well-known video game source code and website source code, complete source code, a variety of games
You have a demo. Please try it out.
5. Instant source code
Instant Messaging (like qq, p2p text, voice, video, file transmission, Remote Assistance)
The server uses the form of completed ports, which can be integrated and connected horizontally, and can accept a large number of concurrent personnel online.
The video has good speech performance. Different versions are available for users to choose from. You are welcome to try it out with a demo.
6. Chat Room source code
A chat room similar to uc has good video and voice effects. There is a demo in a chat room with one or more video windows. You are welcome to try it out.
7. Instant Messaging + chat room source code
Integrated instant communication and chat rooms
Instant Messaging (like qq, p2p text, voice, video, file transmission, Remote Assistance)
Chat rooms are similar to uc
Demo available. Please try it out.
8. Video conferencing source code
Powerful video conferencing source code, including common features of video conferencing, with good video and voice Effects
Demo available. Please try it out.
9. Whiteboard controls
It is convenient for video conferencing, teaching and other software applications.
10. Karaoke DIY source code
If you want to capture the camera and play back the mic, how can you feel your voice?
How can I change the time limits for various functions in the chat room?
Oh, I changed it! You need to change the source code of the file (asp or php. After the change, there will be a lot of gameplay! What name do I forget!