我正在构建一个Android聊天应用程序.我在服务器端使用nodeJs并尝试实现android client for socketIO.我收到了SocketIO异常.这些代码有什么问题?
服务器
var http = require('http'),fs = require('fs');var app = http.createServer(function (req, res) {res.end(); }).Listen(8000, '127.0.0.1');var io = require('socket.io').Listen(app);io.sockets.on('connection', function(socket) {socket.on('echo', function(data) {socket.emit('echoback', data);});});
客户
package com.jack.pri;import java.net.MalformedURLException;import org.Json.JsONException;import org.Json.JsONObject;import androID.os.Bundle;import androID.app.Activity;import androID.util.Log;import androID.vIEw.Menu;import androID.Widget.TextVIEw;import io.socket.*;public class MainActivity extends Activity {//private SocketIO socket;private TextVIEw t;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); t=(TextVIEw) findVIEwByID(R.ID.et1); t.setText("uio"); ////// // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); SocketIO socket = null; try { socket = new SocketIO("http://10.0.2.2:8000"); } catch (MalformedURLException e) { // Todo @R_301_6843@-generated catch block e.printstacktrace(); } socket.connect(new IOCallback() { @OverrIDe public voID on(String event, IOAckNowledge ack, Object... args) { if ("echoback".equals(event) && args.length > 0) { Log.d("SocketIO", "" + args[0]); t.setText("s "); // -> "hello" } } @OverrIDe public voID onMessage(JsONObject Json, IOAckNowledge ack) {} @OverrIDe public voID onMessage(String data, IOAckNowledge ack) {} @OverrIDe public voID one rror(SocketIOException socketIOException) { socketIOException.printstacktrace();} @OverrIDe public voID ondisconnect() {} @OverrIDe public voID onConnect() {} }); socket.emit("echo", "hello"); ///}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true;}}
错误日志
09-10 13:49:34.587: W/System.err(1963): io.socket.socketIOException: Error while handshaking09-10 13:49:34.587: W/System.err(1963): at io.socket.IOConnection.handshake(IOConnection.java:322)09-10 13:49:34.597: W/System.err(1963): at io.socket.IOConnection.access0(IOConnection.java:39)09-10 13:49:34.597: W/System.err(1963): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)09-10 13:49:34.637: W/System.err(1963): Caused by: java.net.socketException: socket Failed: EACCES (Permission denIEd)
解决方法:
检查是否将INTERNET PERMISSION置于Manifest中.如果你不这样做,请:
<uses-permission androID:name="androID.permission.INTERNET"/>
总结 以上是内存溢出为你收集整理的使用Android客户端进行socketIO时出现SocketIO异常全部内容,希望文章能够帮你解决使用Android客户端进行socketIO时出现SocketIO异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)