socketIO服务器,安卓,js端使用代码

socketio的所有jar包百度云盘链接http://pan.baidu.com/s/1kU8VRfx密码jsmj


使用:
服务端:
public class SocketIO {
private SocketIOServer server;
//初始化socketio
public  void initSocetio(String address) {
Configuration config = new Configuration();
config.setHostname(address);ip地址
config.setPort(4567);端口
server = new SocketIOServer(config);
SocetIOConnectListenerListener();
SocetIOListener();
server.start();开启
}

//添加系统内监听

private  void SocetIOListener() {

server.addEventListener("login",String.class, new DataListener() {login为标识,可随意更改,服务器客户端一致即可。String为接收的类型,可更改。DataListener为接收监听。
@Override
public void onData(SocketIOClient client, String data, AckRequest arg2)
throws Exception {
System.out.println(client.getRemoteAddress()+"接收到的信息为:" + data+"\r\n");
client.sendEvent("login", json);login为标识,与客户端应一致
}
});
}
//添加系统外监听
private  void SocetIOPrivilegeListener() {
server.addEventListener("privilege",String.class, new DataListener() {
@Override
public void onData(SocketIOClient client, String data, AckRequest arg2)
throws Exception {
System.out.println(client.getRemoteAddress()+"接收到的系统外信息为:" + data+"\r\n");
int differentcase=jsonUtil.getCase(data);
dateProcessing.ReceivedDate(data,differentcase,client);
}
});
}



//添加连接监听事件,监听是否与客户端连接到服务器
private  void SocetIOConnectListenerListener() {
server.addConnectListener(new ConnectListener() {
public void onConnect(SocketIOClient client) {
// 判断是否有客户端连接
if (client != null) {
System.out.println("\r\n"+client.getRemoteAddress()+"链接成功");
} else {
System.err.println(client.getRemoteAddress()+"链接失败");
}
}
});
}
}




安卓端
public class MainActivity extends Activity {




    //创建socket连接
    private Socket mSocket;
    {
        try {
            mSocket = IO.socket("http://192.168.8.186:4567/");
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        socketConn();
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = etMessage.getText().toString();
                if (mSocket.connected()) {
                    mSocket.emit("login", message);标识   发送的信息
                    //连接服务
                    mSocket.on("login", onLogin);
                } else {
                    Toast.makeText(MainActivity.this, "链接失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mSocket.on("login", onLogin);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSocket.off("login", onLogin);
        //取消连接Server
        mSocket.disconnect();
        mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
        mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
    }


    //初始化控件信息
    private void init() {
        btnSend = (ButtonRectangle) findViewById(R.id.btn_send);
        tvMessage = (TextView) findViewById(R.id.tv_message);
        etMessage = (EditText) findViewById(R.id.et_message);
    }


    //连接到Server
    private void socketConn() {
        mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
        mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
        mSocket.connect();
    }


    private Emitter.Listener onLogin = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            String s = (String) args[0];
s为收到的信息
            }
        }
    };


    private Emitter.Listener onConnectError = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "已断开", Toast.LENGTH_LONG).show();
                }
            });
        }
    };


}




js端
接收
var socketServer = io.connect('http://192.168.8.186:4567/ 
');

    socketServer.on('login', function (data) {

data为接收到的消息

    });

发送:
socketServer.emit("login",JSON.stringify(obj));

你可能感兴趣的:(安卓,后台)