Created
October 29, 2013 21:23
-
-
Save joegle/7222868 to your computer and use it in GitHub Desktop.
A server that starts and stops audio recording with websocket messages
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
package main | |
import ( | |
"net/http" | |
"fmt" | |
"os/exec" | |
"time" | |
"code.google.com/p/go.net/websocket" | |
) | |
// Echo the data received on the WebSocket. | |
func EchoServer(ws *websocket.Conn) { | |
//io.Copy(ws, ws) | |
c := make(chan bool) | |
for { | |
var message string | |
websocket.Message.Receive(ws, &message) | |
if message == "Stop" { | |
c<-true | |
fmt.Println(message) | |
} | |
if message=="Start"{ | |
fmt.Println(message) | |
go newrecording(c) | |
} | |
websocket.Message.Send(ws, "message") | |
} | |
} | |
// This example demonstrates a trivial echo server. | |
func ExampleHandler() { | |
http.Handle("/echo", websocket.Handler(EchoServer)) | |
err := http.ListenAndServe(":12345", nil) | |
if err != nil { | |
panic("ListenAndServe: " + err.Error()) | |
} | |
} | |
func newrecording(c <-chan bool){ | |
filename := fmt.Sprintf("recs/%d.wav",time.Now().Unix()) | |
cmd := exec.Command("arecord","-f","cd","-t","wav",filename) | |
cmd.Start() | |
//fmt.Print(cmd.Process.Pid) | |
select{ | |
case <- c: | |
cmd.Process.Kill() | |
} | |
} | |
func main(){ | |
ExampleHandler() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment