Created
January 21, 2015 20:46
-
-
Save damienstanton/aa81286a44448f87db13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ari = require("ari-client") | |
| util = require("util") | |
| ari.connect("http://10.11.220.37", "asterisk", "Br!dg3", clientLoaded) | |
| clientLoaded = (err, client) -> | |
| if err | |
| throw err | |
| # ------------- | |
| # Entry Handler | |
| stasisStart = (event, channel) -> | |
| # ensure the channel is not a dialed channel | |
| dialed = event.args[0] = "dialed" | |
| if not dialed | |
| chanel.answer(err) -> | |
| if err | |
| throw err | |
| console.log("Channel %s has entered the application...", channel.name) | |
| playback = client.Playback() | |
| channel.play({media: "sound:pls-wait-connect-call"}, | |
| playback, (err, playback) -> | |
| if err | |
| throw err) | |
| originate(channel) | |
| originate = (channel) -> | |
| dialed = client.Channel() | |
| channel.on("StasisEnd", (event, channel) -> | |
| hangupDialed(channel, dialed)) | |
| dialed.on("ChannelDestroyed", (event, dialed) -> | |
| hangupOriginal(channel, dialed)) | |
| dialed.on("StasisStart", (event, dialed) -> | |
| joinMixingBridge(channel, dialed) ) | |
| dialed.originate({endpoint: process.argv[2], app: "appname", appArgs: "dialed"}, | |
| (err, dialed) -> | |
| if err | |
| throw err) | |
| # ------------------------------ | |
| # Hangup Handler (original side) | |
| hangupDialed = (channel, dialed) -> | |
| console.log("Channel %s has exited the application, hanging up the dialed channel...", channel.name, dialed.name) | |
| dialed.hangup((err) ->) | |
| # ---------------------------- | |
| # Hangup Handler (dialed side) | |
| hangupOriginal = (channel, dialed) -> | |
| console.log("Dialed chanel %s is killed, hanging up channel %s", dialed.name, channel.name) | |
| channel.hangup((err) ->) | |
| # --------------------- | |
| # Channel Entry Handler | |
| joinMixingBridge = (channel, dialed) -> | |
| bridge = client.Bridge() | |
| dialed.on("StasisEnd", (event, dialed) -> | |
| dialedExit(dialed, bridge)) | |
| dialed.answer((err) -> | |
| if err | |
| throw err) | |
| bridge.create({type: "mixing"}, (err, bridge) -> | |
| if err | |
| throw err) | |
| console.log("Created bridge %s", bridge.id) | |
| addChannelsToBridge(channel, dialed, bridge) | |
| # -------------------- | |
| # Channel Exit Handler | |
| dialedExit = (dialed, bridge) -> | |
| console.log("Dailed channel %s has left the application, destroying bridge %s...", dialed.name, bridge.id) | |
| bridge.destroy((err) -> | |
| if err | |
| throw err) | |
| # ------------------------- | |
| # New Mixing Bridge Handler | |
| addChannelsToBridge = (channel, dialed, bridge) -> | |
| console.log("Adding channel %s and dialed channel %s to bridge %s", channel.name, dialed.name, bridge.id) | |
| bridge.addChannel({channel: [channel.id, dialed.id]}, (err) -> | |
| if err | |
| throw err) | |
| client.on("StasisStart", stasisStart) | |
| client.start("appname") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment