项目作者: gkathir15

项目描述 :
CoRoutine WorkBench - Kotlin Andorid
高级语言: Kotlin
项目地址: git://github.com/gkathir15/CoroutinesWorkbench.git
创建时间: 2020-04-08T18:43:26Z
项目社区:https://github.com/gkathir15/CoroutinesWorkbench

开源协议:

下载


Coroutine codelab Basics

Intent of the talk

Since AsyncTask is depricated Kotlin corutines is another way of going up for background execution on Android.

This App has covered the the Basic Scopes available in coroutine and launching task to differnet threads.
Slides

  1. dependencies {
  2. ...
  3. implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:x.x.x"
  4. implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:x.x.x"
  5. }

The app has samples to

  • Download image and show in ImageView
  • Connecting to a socket and updating UI
  • Fetching a http Api and parsing using Gson and displaying on UI & also downloading and catching bitmaps in memory.

With regards to socket, the code samples of socket server and a sample client to test is pased below.

Installation

requires Node.js

*server.js

  1. const net = require('net');
  2. const port = 80;
  3. const host = '192.168.0.108';
  4. const server = net.createServer();
  5. server.listen(port, host, () => {
  6. console.log('TCP Server is running on port ' + port + '.');
  7. });
  8. let sockets = [];
  9. server.on('connection', function(sock) {
  10. console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
  11. sockets.push(sock);
  12. let check = true;
  13. sock.on('data', function(data) {
  14. console.log('DATA ' + sock.remoteAddress + ': ' + data);
  15. // Write the data back to all the connected, the client will receive it as data from the server
  16. setInterval(function(){
  17. sockets.forEach(function(sock, index, array) {
  18. sock.write(new Date().toLocaleTimeString());
  19. });},3000)
  20. });
  21. // Add a 'close' event handler to this instance of socket
  22. sock.on('close', function(data) {
  23. let index = sockets.findIndex(function(o) {
  24. return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
  25. })
  26. if (index !== -1) sockets.splice(index, 1);
  27. console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
  28. });
  29. });

to run Server
node server.js

*client.js

  1. var net = require('net');
  2. var client = new net.Socket();
  3. client.connect(80, '192.168.0.108', function() {
  4. console.log('Connected');
  5. client.write('Hello, server! Love, Client.');
  6. });
  7. client.on('data', function(data) {
  8. console.log('Received: ' + data);
  9. //client.destroy(); // kill client after server's response
  10. });
  11. client.on('close', function() {
  12. console.log('Connection closed');
  13. });

to run Client
node client.js

*To Do

open your ports both inbound and outbounf in your windows firewall to connect to Mobile device ie port 80

ipconfig

Use the ipv4 ip and port to connect to socket.