Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace SignalRSelfHost
- {
- class Program
- {
- static void Main(string[] args)
- {
- // This will *ONLY* bind to localhost, if you want to bind to all addresses
- // use http://*:8080 to bind to all addresses.
- // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
- // for more information.
- string url = "http://localhost:8080";
- using (WebApp.Start(url))
- {
- Console.WriteLine("Server running on {0}", url);
- Console.ReadLine();
- }
- }
- }
- class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- app.UseCors(CorsOptions.AllowAll);
- app.MapSignalR();
- }
- }
- public class MessageHub : Hub
- {
- public static event Action<string, string> MessageReceived = delegate { };
- public void SendMessage(string name, string message)
- {
- MessageReceived(name, message);
- }
- }
- public class CustomType
- {
- public string Name;
- public int Id;
- }
- }
- Handler handler;
- TextView statusField;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- handler = new Handler();
- statusField = (TextView) findViewById(R.id.statusField);
- Platform.loadPlatformComponent(new AndroidPlatformComponent());
- // Change to the IP address and matching port of your SignalR server.
- String host = "http://127.0.0.1:8080/";
- HubConnection connection = new HubConnection( host );
- HubProxy hub = connection.createHubProxy( "MessageHub" );
- ClientTransport transport = new ServerSentEventsTransport(connection.getLogger());
- SignalRFuture<Void> awaitConnection = connection.start(transport);
- try {
- awaitConnection.get();
- }
- catch (InterruptedException e) {
- Log.d("CHECK", e.toString());
- e.printStackTrace();
- } catch (ExecutionException e) {
- Log.d("CHECK", e.toString());
- e.printStackTrace();
- }
- hub.subscribe(this);
- try {
- hub.invoke( "SendMessage", "Client", "Hello world!" ).get();
- hub.invoke( "SendCustomType",
- new CustomType() {{ Name = "Universe"; Id = 42; }} ).get();
- } catch (InterruptedException e) {
- // Handle ...
- } catch (ExecutionException e) {
- // Handle ...
- }
- }
- public void UpdateStatus(String status) {
- final String fStatus = status;
- handler.post(new Runnable() {
- @Override
- public void run() {
- statusField.setText(fStatus);
- }
- });
- }
- public class CustomType
- {
- public String Name;
- public int Id;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement