SignalR的Android客户端没有收到,但JS没有收到

SignalR的Android客户端没有收到,但JS没有收到,第1张

概述这是一个“hello world”应用程序,但我找不到解决方案. 这是客户端上的代码: public class MainActivity extends ActionBarActivity { HubProxy proxy; EditText messageText; TextView resultText; @Override protected v 这是一个“hello world”应用程序,但我找不到解决方案.

这是客户端上的代码:

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

我认为错误在于此代码,因此我不附加服务器和Js客户端的代码.

解决方法 您可以参考我的以下示例代码.我测试过了.希望这可以帮助!

public class MainActivity extends AppCompatActivity {private TextVIEw mTextVIEw;private final Context mContext = this;private Handler mHandler;@OverrIDeprotected 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);}...}
总结

以上是内存溢出为你收集整理的SignalR的Android客户端没有收到,但JS没有收到全部内容,希望文章能够帮你解决SignalR的Android客户端没有收到,但JS没有收到所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127912.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存