In this tutorial, I will show how to use Java-Websocket Library together with Kotlin. It will be a simple app. The app opens a connection to the Coinbase Pro Websocket Feed by using Java-Websocket. When the connection is open the app will get the realtime Bitcoin price in Euro from Coinbase Pro. This is a step by step tutorial.
在本教程中,我将展示如何与Kotlin一起使用Java-Websocket库。 这将是一个简单的应用程序。 该应用程序使用Java-Websocket打开与Coinbase Pro Websocket Feed的连接。 打开连接后,应用程序将从Coinbase Pro获取欧元的实时比特币价格。 这是一个循序渐进的教程。
先决条件 (Prerequisites)
You should already be familiar with Kotlin and Android development. I am using Android Studio 4.0.1. You should also know that a connection to a web socket is bidirectional. Once the connection is open the communication can happen to both sides from server to client or client to server. In case you are more interested in the differences between HTTP and web sockets, I recommend watching this YouTube video.
您应该已经熟悉Kotlin和Android开发。 我正在使用Android Studio 4.0.1。 您还应该知道与Web套接字的连接是双向的。 连接打开后,服务器与客户端之间或客户端与服务器之间的通信就会发生。 如果您对HTTP和网络套接字之间的差异更感兴趣,建议您观看此YouTube视频。
为什么写这篇文章? (Why this article?)
I recently had a coding challenge for a company where I had to use web sockets to get realtime stock prices. Because of the outdated resources and hard to use libraries. I thought there is a simple tutorial needed so every Android developer can easily use web sockets in 2020.
最近,对于一家不得不使用Web套接字获取实时股价的公司,我遇到了编码挑战。 由于资源过时且难以使用的库。 我认为需要一个简单的教程,以便每个Android开发人员都可以在2020年轻松使用Web套接字。
该应用程序将是什么样 (What the app will look like)
The app will only contain a TextView on the main screen which shows the current Bitcoin price on Coinbase Pro in Euro.
该应用程序将仅在主屏幕上包含一个TextView,该文本视图显示欧元在Coinbase Pro上的当前比特币价格。

