Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.socket;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import java.net.URISyntaxException;
- import io.socket.client.IO;
- import io.socket.client.Socket;
- import io.socket.emitter.Emitter;
- public class MainActivity extends AppCompatActivity {
- private static final String TAG = "Main activity";
- public Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) findViewById(R.id.button);
- View.OnClickListener Click = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- SocketIO socketIO = new SocketIO();
- socketIO.run();
- }
- };
- button.setOnClickListener(Click);
- }
- public class SocketIO implements Runnable {
- @Override
- public void run() {
- final Socket mSocket;
- try {
- mSocket = IO.socket("http://127.0.0.1:8080/");
- mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
- @Override
- public void call(Object... args) {
- Log.d(TAG, "connect success");
- mSocket.emit("ferret", "tobi");
- mSocket.disconnect();
- }
- }).on("event", new Emitter.Listener() {
- @Override
- public void call(Object... args) {
- }
- }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
- @Override
- public void call(Object... args) {
- }
- }).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
- @Override
- public void call(Object... args) {
- Log.e(TAG, "Error connect");
- }
- }).on(Socket.EVENT_CONNECTING, new Emitter.Listener() {
- @Override
- public void call(Object... args) {
- Log.d(TAG, "Connecting");
- }
- }).on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
- @Override
- public void call(Object... args) {
- Log.d(TAG, "Connect timeout");
- }
- });
- mSocket.connect();
- } catch (URISyntaxException UTE) {
- Log.e(TAG, UTE.toString());
- }
- }
- }
- }
- D/Main activity: Connecting
- E/Main activity: Error connect
- var app = require('express')();
- var server = require('http').Server(app);
- var io = require('socket.io')(server);
- server.listen(8080);
- app.get('/', function (req, res) {
- res.sendfile(__dirname + '/page/index.html');
- });
- io.on('connection', function (socket) {
- console.log('connect user:' + socket.id);
- socket.on('ferret', function (name, fn) {
- fn('dsfhdsjfhdkjshfkjdshfkjd');
- });
- socket.on('disconnect', function () {
- io.emit('user disconnected');
- console.log('disconnect user:' + socket.id);
- });
- });
Add Comment
Please, Sign In to add comment