本文介绍了Android客户端为SignalR没有收到,但JS确实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的问题是非常简单的。这是作为一个Hello World应用程序,但我不能找到解决办法。我用Google搜索非常喜欢,但没有!

The Question is very simple. It is as a hello world app but i can't find the solution. I have googled it very much but nothing!

这是客户端上的code:

This is the code on the client:

public class MainActivity extends ActionBarActivity {

    HubProxy proxy;
    EditText messageText;
    TextView resultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Platform.loadPlatformComponent(new AndroidPlatformComponent());

        //Setup connection
        String server = "http://10.0.2.2:8081/";
        HubConnection connection = new HubConnection(server);
        proxy = connection.createHubProxy("chatHub");

        Toast.makeText(getApplicationContext(),proxy.toString(),Toast.LENGTH_SHORT).show();

        resultText = (TextView) findViewById(R.id.resultText);

        //Start connection
        SignalRFuture<Void> awaitConnection = connection.start();

        //--HERE IS WHERE I AM NOT SURE HOW TO SET IT UP TO RECEIVE A NOTIFICATION---
        //Setup to receive broadcast message
        proxy.on("SimpleMessage", new SubscriptionHandler1<String>() {
            @Override
            public void run(String s) {
                resultText.setText(resultText.getText()+"\n"+s.toString());
                Toast.makeText(getApplicationContext(), "message recieved: "+s.toString(), Toast.LENGTH_LONG).show();
            }
        }
                , String.class);

        proxy.on("Alert", new SubscriptionHandler() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "Alert Recieved!!!", Toast.LENGTH_LONG).show();
                //resultText.setText(resultText.getText()+"\nAlert Recieved!!!");
            }
        });
        //---------------------------------------------------------------------------

        proxy.subscribe(this);

        try {
            awaitConnection.get();
            Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
        } catch (InterruptedException e) {
            Toast.makeText(getApplicationContext(), "un success", Toast.LENGTH_LONG).show();
        } catch (ExecutionException e) {
            Toast.makeText(getApplicationContext(), "in success: " + e.getMessage().toString(), Toast.LENGTH_LONG).show();
        }

        messageText = (EditText) findViewById(R.id.messageText);

        Button sendBTN = (Button) findViewById(R.id.sendBTN);

        sendBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String textToSend = messageText.getText().toString();
                    proxy.invoke("Send", "Android", textToSend ).get();
                } catch (InterruptedException e) {
                    // Handle ...
                } catch (ExecutionException e) {
                    // Handle ...
                }
            }
        });
    }

    public void Alert(){
        Toast.makeText(getApplicationContext(), "Alert Recieved!!!", Toast.LENGTH_LONG).show();
        //resultText.setText(resultText.getText()+"\nAlert Recieved!!!");
    }


        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我认为错误是在这个code,因此我不附加在服务器和客户端的JS的code。所以,请告诉我任何事情可以帮我!

I think the mistake is in this code and therefore i do not append the code of the server and the JS client. So please tell me any thing may help me!

在此先感谢。

推荐答案

您可以参考我下面的示例code。我已经测试。希望这有助于!

You can refer to my following sample code. I have tested. Hope this helps!

public class MainActivity extends AppCompatActivity {

private TextView mTextView;
private final Context mContext = this;
private Handler mHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView) findViewById(R.id.textView);

    mHandler = new Handler(Looper.getMainLooper());

    Platform.loadPlatformComponent(new AndroidPlatformComponent());
    Credentials credentials = new Credentials() {
        @Override
        public void prepareRequest(Request request) {
            request.addHeader("User-Name", "BNK");
        }
    };
    String serverUrl = "http://192.168.1.100/signalr";
    HubConnection connection = new HubConnection(serverUrl);
    connection.setCredentials(credentials);
    HubProxy hubProxy = connection.createHubProxy("ChatHub");
    ClientTransport clientTransport = new ServerSentEventsTransport(connection.getLogger());
    SignalRFuture<Void> awaitConnection = connection.start(clientTransport);
    try {
        awaitConnection.get();
    } catch (InterruptedException | ExecutionException e) {
        Log.e("SignalR", e.toString());
        e.printStackTrace();
        return;
    }

    hubProxy.on("SimpleMessage", new SubscriptionHandler1<String>() {
        @Override
        public void run(final String s) {
            Log.i("SignalR", s);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mTextView.setText(mTextView.getText() + "\n" + s);
                    Toast.makeText(mContext, "message recieved: " + s, Toast.LENGTH_LONG).show();
                }
            });
        }
    }, String.class);
}

...
}

这篇关于Android客户端为SignalR没有收到,但JS确实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 08:21