Android client for SignalR does not receive but JS does

家住魔仙堡 提交于 2019-12-07 08:45:51

问题


This is a "hello world" app but I can't find the solution.

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);
    }
}

I think the mistake is in this code and therefore I do not append the code of the server and the JS client.


回答1:


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);
}

...
}


来源:https://stackoverflow.com/questions/32656767/android-client-for-signalr-does-not-receive-but-js-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!