func main() { db, err = sql.Open("mysql", "root:haosoo8888@tcp(127.0.0.1:3306)/call?charset=utf8") if err != nil { log.Fatal(err) } db.SetMaxOpenConns(2000) db.SetMaxIdleConns(1000) defer db.Close() err = db.Ping() if err != nil { fmt.Println("数据库连接错误!") } pool = newPool(redisServer) // Init a syslog writter for our test l, errLog := syslog.New(syslog.LOG_INFO, "TestFSock") if errLog != nil { l.Crit(fmt.Sprintf("Cannot connect to syslog:", errLog)) return } // No filters evFilters := map[string]string{} // We are interested in heartbeats, define handler for them evHandlers := map[string][]func(string){"CHANNEL_CREATE": []func(string){printChannelCreate}, "CHANNEL_ANSWER": []func(string){printChannelAnswer}, "CHANNEL_HANGUP_COMPLETE": []func(string){printChannelHungup}} // evHandlers := map[string][]func(string){"CHANNEL_CREATE": []func(string){printChannelCreate}} fs, err := fsock.NewFSock("127.0.0.1:8021", "ClueCon", 10, evHandlers, evFilters, l) if err != nil { l.Crit(fmt.Sprintf("FreeSWITCH error:", err)) return } fs.ReadEvents() }
// Connects to the freeswitch mod_event_socket server and starts // listening for events. func (sm *FSSessionManager) Connect() (err error) { eventFilters := map[string]string{"Call-Direction": "inbound"} if fsock.FS, err = fsock.NewFSock(cfg.FreeswitchServer, cfg.FreeswitchPass, cfg.FreeswitchReconnects, sm.createHandlers(), eventFilters, engine.Logger.(*syslog.Writer)); err != nil { return err } else if !fsock.FS.Connected() { return errors.New("Cannot connect to FreeSWITCH") } fsock.FS.ReadEvents() return errors.New("<SessionManager> - Stopped reading events") }
// Connects to the freeswitch mod_event_socket server and starts // listening for events. func (sm *FSSessionManager) Connect() error { eventFilters := map[string]string{"Call-Direction": "inbound"} errChan := make(chan error) for _, connCfg := range sm.cfg.Connections { connId := utils.GenUUID() fSock, err := fsock.NewFSock(connCfg.Server, connCfg.Password, connCfg.Reconnects, sm.createHandlers(), eventFilters, utils.Logger.(*syslog.Writer), connId) if err != nil { return err } else if !fSock.Connected() { return errors.New("Could not connect to FreeSWITCH") } else { sm.conns[connId] = fSock } go func() { // Start reading in own goroutine, return on error if err := sm.conns[connId].ReadEvents(); err != nil { errChan <- err } }() if fsSenderPool, err := fsock.NewFSockPool(5, connCfg.Server, connCfg.Password, 1, make(map[string][]func(string, string)), make(map[string]string), utils.Logger.(*syslog.Writer), connId); err != nil { return fmt.Errorf("Cannot connect FreeSWITCH senders pool, error: %s", err.Error()) } else if fsSenderPool == nil { return errors.New("Cannot connect FreeSWITCH senders pool.") } else { sm.senderPools[connId] = fsSenderPool } if sm.cfg.ChannelSyncInterval != 0 { // Schedule running of the callsync go func() { for { // Schedule sync channels to run repetately time.Sleep(sm.cfg.ChannelSyncInterval) sm.SyncSessions() } }() } } err := <-errChan // Will keep the Connect locked until the first error in one of the connections return err }