Guest User

Untitled

a guest
Jul 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. package com.example.socket;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. import java.net.URISyntaxException;
  10.  
  11. import io.socket.client.IO;
  12. import io.socket.client.Socket;
  13. import io.socket.emitter.Emitter;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16. private static final String TAG = "Main activity";
  17.  
  18. public Button button;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24.  
  25. button = (Button) findViewById(R.id.button);
  26.  
  27. View.OnClickListener Click = new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. SocketIO socketIO = new SocketIO();
  31. socketIO.run();
  32. }
  33. };
  34.  
  35. button.setOnClickListener(Click);
  36. }
  37.  
  38.  
  39.  
  40. public class SocketIO implements Runnable {
  41. @Override
  42. public void run() {
  43.  
  44. final Socket mSocket;
  45.  
  46. try {
  47. mSocket = IO.socket("http://127.0.0.1:8080/");
  48.  
  49. mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
  50.  
  51. @Override
  52. public void call(Object... args) {
  53. Log.d(TAG, "connect success");
  54. mSocket.emit("ferret", "tobi");
  55. mSocket.disconnect();
  56. }
  57.  
  58. }).on("event", new Emitter.Listener() {
  59.  
  60. @Override
  61. public void call(Object... args) {
  62.  
  63. }
  64.  
  65. }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
  66.  
  67. @Override
  68. public void call(Object... args) {
  69.  
  70. }
  71.  
  72. }).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
  73. @Override
  74. public void call(Object... args) {
  75. Log.e(TAG, "Error connect");
  76. }
  77. }).on(Socket.EVENT_CONNECTING, new Emitter.Listener() {
  78. @Override
  79. public void call(Object... args) {
  80. Log.d(TAG, "Connecting");
  81. }
  82. }).on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
  83. @Override
  84. public void call(Object... args) {
  85. Log.d(TAG, "Connect timeout");
  86. }
  87. });
  88.  
  89. mSocket.connect();
  90. } catch (URISyntaxException UTE) {
  91. Log.e(TAG, UTE.toString());
  92. }
  93. }
  94. }
  95. }
  96.  
  97. D/Main activity: Connecting
  98. E/Main activity: Error connect
  99.  
  100. var app = require('express')();
  101. var server = require('http').Server(app);
  102. var io = require('socket.io')(server);
  103.  
  104. server.listen(8080);
  105.  
  106. app.get('/', function (req, res) {
  107. res.sendfile(__dirname + '/page/index.html');
  108. });
  109.  
  110. io.on('connection', function (socket) {
  111. console.log('connect user:' + socket.id);
  112. socket.on('ferret', function (name, fn) {
  113. fn('dsfhdsjfhdkjshfkjdshfkjd');
  114. });
  115.  
  116. socket.on('disconnect', function () {
  117. io.emit('user disconnected');
  118. console.log('disconnect user:' + socket.id);
  119. });
  120. });
Add Comment
Please, Sign In to add comment