Created
February 2, 2014 02:06
-
-
Save cdeutsch/8762010 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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using MonoTouch.Foundation; | |
| using MonoTouch.UIKit; | |
| using Alchemy; | |
| using Alchemy.Classes; | |
| using System.Net.Http; | |
| using Newtonsoft.Json; | |
| namespace WebSocketTest | |
| { | |
| // The UIApplicationDelegate for the application. This class is responsible for launching the | |
| // User Interface of the application, as well as listening (and optionally responding) to | |
| // application events from iOS. | |
| [Register ("AppDelegate")] | |
| public partial class AppDelegate : UIApplicationDelegate | |
| { | |
| // class-level declarations | |
| UIWindow window; | |
| WebSocketTestViewController viewController; | |
| WebSocketClient client; | |
| // | |
| // This method is invoked when the application has loaded and is ready to run. In this | |
| // method you should instantiate the window, load the UI into it and then make the window | |
| // visible. | |
| // | |
| // You have 17 seconds to return from this method, or iOS will terminate your application. | |
| // | |
| public override bool FinishedLaunching (UIApplication app, NSDictionary options) | |
| { | |
| window = new UIWindow (UIScreen.MainScreen.Bounds); | |
| viewController = new WebSocketTestViewController (); | |
| window.RootViewController = viewController; | |
| window.MakeKeyAndVisible (); | |
| go (); | |
| return true; | |
| } | |
| async private void go() { | |
| var httpClient = new HttpClient(); | |
| var result = httpClient.PostAsync("http://127.0.0.1:3000/socket.io/1", null); | |
| var auth = await result.Result.Content.ReadAsStringAsync(); | |
| var token = auth.Split (':') [0]; | |
| //httpClient.PostAsync("", token + ":15:25:websocket,flashsocket,htmlfile,xhr-polling,jsonp-polling");"); | |
| Console.WriteLine (auth); | |
| Console.WriteLine (token); | |
| //var prov = JsonConvert.DeserializeObject<List<Province>>(json); | |
| client = new WebSocketClient("ws://127.0.0.1:3000/socket.io/1/websocket/" + token) { | |
| //Origin = "127.0.0.1", //TODO: | |
| OnReceive = OnClientReceive, | |
| OnConnect = OnClientConnect, | |
| OnConnected = OnClientConnected, | |
| OnDisconnect = OnClientDisconnect | |
| }; | |
| client.Connect(); | |
| } | |
| private void OnClientDisconnect(UserContext context) | |
| { | |
| Console.WriteLine("disconnect"); | |
| } | |
| private void OnClientConnect(UserContext context) | |
| { | |
| Console.WriteLine("connect"); | |
| Console.WriteLine(client.ReadyState.ToString()); | |
| client.Send("fuck"); | |
| } | |
| private void OnClientConnected(UserContext context) | |
| { | |
| Console.WriteLine("connected"); | |
| Console.WriteLine(client.ReadyState.ToString()); | |
| // send data. | |
| //client.Send("PUB _s61748_d201a456e3bd325bbf0fdc0d5c0aa3e09c820b4f|{\"event\":\"activePlayer\",\"name\":\"_web_3179356\",\"playState\":0,\"volume\":0.37333333333333335}"); | |
| //client.Send("CONNECT s61748_d201a456e3bd325bbf0fdc0d5c0aa3e09c820b4f"); | |
| //client.Send("SUB s61748"); | |
| //client.Send("SUB s61748/fields"); | |
| //client.Send("SUB _s61748_d201a456e3bd325bbf0fdc0d5c0aa3e09c820b4f"); | |
| //client.Send("PUB _s61748_d201a456e3bd325bbf0fdc0d5c0aa3e09c820b4f|{\"event\":\"masterQuery\"}"); | |
| //client.Send("PUB _s61748_d201a456e3bd325bbf0fdc0d5c0aa3e09c820b4f|{\"event\":\"remote\",\"type\":\"playPause\"}"); | |
| } | |
| private void OnClientReceive(UserContext context) | |
| { | |
| var data = context.DataFrame.ToString(); | |
| Console.WriteLine("receive"); | |
| Console.WriteLine(data); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment