示例#1
0
 static void Main(string[] args)
 {
     //first auth spotifyapi;
     spot = new SpotifyAPI(SpotifyAPI.GetOAuth(), "localhost");
     //now start the spotify checker thread.
     server.StartServer(spot);
 }
示例#2
0
        public static void OnRecievedData(IAsyncResult ar)
        {
            SpotifyWebClient client = (SpotifyWebClient)ar.AsyncState;
            byte[] aryRet = client.GetRecievedData(ar);

            if (aryRet.Length < 1)
            {
                Console.WriteLine(client.ReadOnlySocket.RemoteEndPoint + " has left.");
                client.ReadOnlySocket.Close();
                ClientsList.Remove(client);
                return;
            }
            string data = Encoding.ASCII.GetString(aryRet);
            string[] headers = data.Split('\n');
            string[] get = headers[0].Split(' ');
            if (get.Length != 3) { client.ReadOnlySocket.Send(Encoding.ASCII.GetBytes("Invalid Request.")); client.ReadOnlySocket.Close(); }
            //get the GET shit.
            //list commands here.
            try
            {
                Responses.CFID cfid = spot.CFID;
                switch (get[1])
                {
                    case "/play":
                        Console.WriteLine("Play");
                        var f = spot.Resume;
                        Send(client.ReadOnlySocket, "Playing.");
                        break;
                    case "/pause":
                        Console.WriteLine("Pause");
                        var fg = spot.Pause;
                        Send(client.ReadOnlySocket, "Paused.");
                        break;
                    case "/next":
                        Console.WriteLine("Next");
                        controller.PlayNext();
                        Send(client.ReadOnlySocket, "Next.");
                        break;
                    case "/prev":
                        controller.PlayPrev();
                        Send(client.ReadOnlySocket, "Prev.");
                        break;
                    case "/volup":
                        controller.VolumeUp();
                        Send(client.ReadOnlySocket, "VolUp.");
                        break;
                    case "/voldwn":
                        controller.VolumeDown();
                        Send(client.ReadOnlySocket, "VolDown.");
                        break;
                    case "/live":
                        Send(client.ReadOnlySocket, "HTTP/1.1 200 OK\r\nServer: SpotifyWebServer\r\nTransfer-Encoding : chunked\r\nContent-Type : audio/wav\r\nConnection: close\r\n\r\n", false);
                        //Live Streaming not implemented yet. In progress.
                        break;
                    case "/":
                        Send(client.ReadOnlySocket, File.ReadAllText("page.html"));
                        break;
                    default:
                        //check for question mark.
                        if(get[1].Contains("?"))
                        {
                            status = spot.Status;
                            //Build our string.
                            Send(client.ReadOnlySocket, buildreq(status, get[1].Split('?')[1]));
                            return;
                        }
                        //Send(client.ReadOnlySocket, "Invalid Request.");
                        //Try to load the file.
                        try
                        {
                        byte[] m = File.ReadAllBytes(Environment.CurrentDirectory + get[1].Replace("/", "\\"));
                        Send(client.ReadOnlySocket, m);
                        }
                        catch(FileNotFoundException ex)
                        {
                            Send(client.ReadOnlySocket, "HTTP/1.1 404 NOT FOUND\r\n\r\n");
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.StartsWith("Object"))
                {
                    //Restart Spotify.
                    spot = new SpotifyAPI(SpotifyAPI.GetOAuth(), "localhost");
                }
                Send(client.ReadOnlySocket, ex.Message);
            }
        }
示例#3
0
 public bool StartServer(SpotifyAPI spotty)
 {
     spot = spotty;
     StartMainServer(80);
     //Recording thread.
     return true;
 }