当前位置: 首页 > 编程日记 > 正文

学习使用React和Electron一次构建自己的桌面聊天应用程序

by Alex Booker

通过亚历克斯布克

学习使用React和Electron一次构建自己的桌面聊天应用程序 (Learn to build your own desktop chat app with React and Electron, one step at a time)

This tutorial was written in collaboration with the awesome Christian Nwamba.

本教程是与令人敬畏的Christian Nwamba合作编写的。

When learning to code, you have a bunch of resources at your disposal — books, screencasts, tutorials, even exercises. But to become a great developer, you need to practice what you learn with a project.

学习编码时,您可以使用大量资源-书籍,截屏视频,教程甚至练习。 但是要成为一名出色的开发人员,您需要练习如何通过项目来学习。

Learning by doing is the motivation for this article. You’ll start from nothing and build a complete chat application, step by step.

边干边学是本文的动机。 您将从零开始,逐步构建一个完整的聊天应用程序。

You’ll build the project one step at a time and to test you understand it, we included some special bonus challenges at the end. Are you up to the test?

您一次将构建一个项目,并测试您是否理解它,最后我们还包括一些特殊的奖金挑战。 您要接受考试吗?

Here’s what we’ll build:

这是我们要构建的:

Pretty cool, right ??

很酷吧?

When you follow along, you’ll learn how to build realtime chat, a “who’s online” list and along the way, how to structure a React app. To power our chat, we’ll be using a service I am helping to build called Chatkit.

当您继续学习时,您将学习如何建立实时聊天,“谁在线”列表,以及如何构建React应用。 为了增强我们的聊天功能,我们将使用一种我正在帮助构建的服务,称为Chatkit 。

Sound good? Tune in to FreeCodeCamp radio for some smooth, gentle background music to keep focused and let’s go!

听起来不错? 收看FreeCodeCamp广播,获取一些平稳,柔和的背景音乐,以保持专注并开始吧!

你需要知道的 (What you need to know)

Actually, one more thing ?:

其实还有一件事吗?:

It would be nice if you knew some basic JavaScript, Node and React already. That being said, if you don’t feel comfortable with these technologies, have a go anyway!

如果您已经了解一些基本JavaScript,Node和React,那就太好了。 话虽这么说,如果您对这些技术不满意,那就去吧!

We intentionally structured this tutorial to make it clear where to paste code so you can follow along. If you have questions, drop them here!

我们故意设计了本教程,以使您清楚地将代码粘贴到哪里,以便您可以继续学习。 如果您有任何疑问,请将其放在这里!

Alright, step one:

好吧,第一步:

设置电子 (Setup Electron)

To build a cross-platform desktop app with web technologies, we’ll use Electron.

要使用网络技术构建跨平台的桌面应用程序,我们将使用Electron 。

To get us off the ground, we made a minimal starter template. Download it:

为了使我们起步,我们制作了一个最小的入门模板。 下载它:

git clone https://github.com/pusher/electron-desktop-starter-template electron-desktop-chat
cd electron-desktop-chat

And install those local dependencies:

并安装这些本地依赖项:

npm install

创建一个Chatkit帐户 (Create a Chatkit account)

We’re not too concerned with building a back-end in this tutorial so we’ll use Chatkit.

在本教程中,我们不太在意构建后端,因此我们将使用Chatkit 。

To follow along, create a free account and a new instance called “Electron desktop chat”:

接下来, 创建一个免费帐户和一个名为“ Electron桌面聊天”的新实例:

In the settings window, turn on the test token provider:

在设置窗口中,打开测试令牌提供程序:

Note Your Test Token Provider Endpoint, Instance Locator and Secret Key. We’ll need them in the next step.

注意您的测试令牌提供者端点实例定位符秘密密钥 。 下一步,我们将需要它们。

设置节点服务器 (Setup the Node server)

Chatkit has two fundamental concepts: Users and rooms.

Chatkit有两个基本概念: 用户和房间 。

Users can create rooms, join them, and chat in them. But before a user can interact with a room, we need to create one.

用户可以创建房间,加入房间并在其中聊天。 但是在用户可以与房间互动之前,我们需要创建一个房间。

This has to happen on the server.

这必须在服务器上发生。

In electron-desktop-chat, run:

在电子桌面聊天中,运行:

npm install --save express cors body-parser pusher-chatkit-server

And paste this in a new file called server.js:

并将其粘贴到名为server.js的新文件中:

Remember to replace instanceLocator and key with your own the Instance Locator and Key.

记住要用您自己的Instance LocatorKey替换instanceLocator和key。

Most of this code is boilerplate, importing dependencies, setting up Express, and so on.

这些代码大部分是样板文件,导入依赖项,设置Express等。

The important part is the “/users” route which handles requests to create a new user.

重要的部分是“ /用户”路由,该路由处理创建新用户的请求。

Run the server with node server.js and you will see the server is “Running on port 3001”.

使用node server.js运行服务器,您将看到服务器正在“在端口3001上运行”。

创建用户名表格 (Create the username form)

When someone loads our app, we’ll want to ask them for a username and then send it to “/users”.

当有人加载我们的应用程序时,我们将要求他们提供用户名,然后将其发送给“ / users”。

Install some native-looking UI components with:

使用以下命令安装一些具有本机外观的UI组件:

npm install --save react-desktop

And create a form component called UsernameForm:

并创建一个名为UsernameForm的表单组件:

You can read more about React form components here. By chance, the documentation uses a NameForm class similar to ours so it should all be familiar!

您可以在此处有关React表单组件的信息 。 偶然地,文档使用与我们类似的NameForm类,因此应该都熟悉!

Next, replace all of App with:

接下来,将所有应用替换为:

And to test it, run npm run dev. You will see the username form is rendered:

要测试它,请运行npm run dev 。 您将看到呈现用户名表单:

Make sure server is running (remember, the command is`node server.js), hit Submit, and you will see that a user is created:

确保服务器正在运行(请记住,命令是node server.js ),单击Submit ,您将看到已创建用户:

在没有库的情况下转换屏幕 (Transitioning screens without a library)

Once we have a user, we can transition them from the UsernameForm to the actual chat screen. We should define that now.

有了用户后,我们可以将其从UsernameForm转换为实际的聊天屏幕。 我们现在应该定义它。

Create a new component called Chat:

创建一个名为Chat的新组件:

And update App:

并更新应用程序:

Run the app, enter a username, and you will transition to the chat screen:

运行应用程序,输入用户名,然后您将切换到聊天屏幕:

使用Chatkit添加实时聊天 (Add realtime chat with Chatkit)

Things are really coming along, wouldn’t you say?

事情真的来了,你不说吗?

To connect to Chatkit from the client, you need to install @pusher/chatkit:

要从客户端连接到Chatkit,您需要安装@ pusher / chatkit :

npm install --save add @pusher/chatkit

And replace all of Chat with:

并将所有Chat替换为:

Remember to replace the tokenProviderUrl and instanceLocator values with Your Test Token Provider Endpoint and Instance Locator.

请记住用您的测试令牌提供者端点实例定位符替换tokenProviderUrlinstanceLocator值。

Run the app, hit ⌘+⌥+I (Control+Shift+I) and you will see that you connected to Chatkit.

运行该应用程序,按⌘+⌥+ I(Control + Shift + I),您将看到已连接到Chatkit。

创建聊天室 (Create a Chatkit room)

We have a user but now we need a room!

我们有一个用户,但现在我们需要一个房间!

To create one, use the Chatkit inspector:

要创建一个,请使用Chatkit检查器:

Remember to copy your room ID, we’ll need this in the next step.

记住要复制您的房间ID,下一步将需要此ID。

创建聊天室组件 (Create a chat room component)

Now we have a room, we can subscribe to new messages sent in that room.

现在我们有了一个房间,我们可以订阅在该房间中发送的新消息。

To render them, create a MessageList component:

要渲染它们,请创建一个MessageList组件:

And update Chat:

并更新聊天:

As ever, remember to update roomId with your actual room ID.

与以往一样,请记住用您的实际房间ID更新roomId

Now, when messages are sent in our room, onNewMessage will be called. From here, we can update our state and in turn, our UI.

现在,当消息发送到我们的房间时,将调用onNewMessage 。 从这里,我们可以更新状态,然后更新我们的UI。

In a second, we will let users send their own messages. For now, to test the subscription, use the inspector:

稍后,我们将让用户发送自己的消息。 现在,要测试订阅,请使用检查器:

传送讯息 (Sending messages)

To let users send their own messages, create a SendMessageForm component:

要让用户发送自己的消息,请创建一个SendMessageForm组件:

And update Chat:

并更新聊天:

Reload the app with ⌘+R (Control+Shift+R) and you will be able to send messages:

用⌘+ R(Control + Shift + R)重新加载应用程序,您将能够发送消息:

In fact, why not open two applications side-by-side and have a conversation with yourself?

实际上,为什么不并排打开两个应用程序并与自己进行对话呢?

What a time to be alive…

多么活着的时间...

设置界面样式 (Style the UI)

Thanks to React Desktop, our app already looks decent but we can do better.

感谢React Desktop ,我们的应用程序看起来已经不错了,但是我们可以做得更好。

Let’s make a few tweaks and define our layout for use in the next and final step.

让我们进行一些调整,并定义我们的布局以供下一步和最后一步使用。

Replace all of index.css with:

将所有index.css替换为:

显示谁在线 (Show who’s online)

To finish our chat app, we will add a “who’s online” list to show, you guessed it, who’s online!

要完成我们的聊天应用程序,我们将添加一个“谁在线”列表,以显示您猜到了谁在线!

Create an OnlineList component:

创建一个OnlineList组件:

Then update Chat:

然后更新聊天:

Were you expecting that to be more difficult? I definitely did the first time I tried!

您是否希望这会更加困难? 我绝对是第一次尝试!

Because Chatkit updates the users property dynamically, we don’t have to manage any of our own state. We simply need to tell React to re-render and, in turn, re-evaluate users each time someone comes online ( onUserCameOnline ), goes offline (onUserWentOffline) or joins the room (onUserJoined).

因为Chatkit动态更新了users属性,所以我们不必管理自己的任何状态。 我们只需要告诉React重新渲染,然后每次有人上线( onUserCameOnline ),下线( onUserWentOffline )或加入会议室( onUserJoined )时重新评估users

Go ahead, run the server and client and admire your magnificent new chat app:

继续,运行服务器和客户端,欣赏您壮观的新聊天应用程序:

If you’ve come this far, you might as well keep going ?! Have a stab at these bonus challenges…

如果您走了这么远,不妨继续前进!! 刺杀这些奖金挑战……

挑战1:显示房间中谁在积极打字 (Challenge 1: Show who in the room is actively typing)

See if you can add typing indicators the app. For example, if am typing, you and everyone else in the room would see “Booker is typing…”.

查看是否可以在应用程序中添加打字指示符。 例如,如果您正在打字,您和房间中的其他所有人都会看到“ Booker正在打字……”。

If Chris and I were both typing, you would see “Booker and Chris are typing…” and so on.

如果Chris和我都在打字,您会看到“ Booker和Chris在打字……”,依此类推。

Hints:

提示:

  • Chatkit typing indicator documentation

    Chatkit打字指示器文档

  • Slack clone tutorial

    松弛克隆教程

挑战2:允许用户创建自己的房间 (Challenge 2: Allow the user to create their own room)

In this tutorial, we used the inspector to create a room and then hard-coded the room ID. This isn’t a good practice.

在本教程中,我们使用检查器创建了一个房间,然后对房间ID进行了硬编码。 这不是一个好习惯。

In most applications, you would want to dynamically create rooms with the createRoom function.

在大多数应用程序中,您需要使用createRoom函数动态创建房间。

When someone loads the app, ask them for a room name as well:

当某人加载该应用程序时,还要求他们提供房间名称:

If the room exists, join it; if not, create it then join it.

如果房间存在,请加入; 如果没有,请创建它,然后加入它。

Hints:

提示:

  • createRoom documentation

    createRoom文档

结论 (Conclusion)

That was fun! We built a pretty cool chat app in what, less than an hour?

那很有趣! 我们用不到一个小时的时间构建了一个非常酷的聊天应用程序?

One thing I noticed (and maybe you did too) is that once the foundation is in place (template, Chatkit connection, and so on), adding new chat features is pretty simple.

我注意到的一件事(也许您也这样做了)是,一旦基础就位(模板,Chatkit连接等),添加新的聊天功能就非常简单。

Aside from the challenges, we would be curious to see where else you could take the application. Some ideas:

除了挑战之外,我们很想知道您还可以将应用程序带到哪里。 一些想法:

  • Send files

    发送文件
  • Unread message count

    未读邮件数
  • Notifications

    通知事项
  • Read cursors

    读取游标
  • Private one-to-one messaging

    私人一对一消息传递

Feel free to follow us on Twitter, @bookercodes and @codebeast.

随时在Twitter, @ bookercodes和@codebeast上关注我们。

Until next time, ciao.

直到下次,再见。

翻译自: https://www.freecodecamp.org/news/build-a-desktop-chat-app-with-react-electron-and-chatkit-744d168e6f2f/

相关文章:

数据库删除,存储

布局主要分两个 其中主布局是 <?xml version"1.0" encoding"utf-8"?> <EditText android:id"id/yf_input" android:layout_width"wrap_content" android:layout_height"wrap_content" android:hint"" …

【Ant Design Pro 一】 环境搭建,创建一个demo

技术交流qq群 173683895 搭建 Ant Design Pro 的前期准备&#xff1a;你的本地环境需要安装 cnpm、node。 注&#xff1a;代码块中的 $ 代表&#xff1a; $后面是在命令行输入的命令&#xff0c;举例 $ npm start 解&#xff1a;实际上是应该打开命令行输入 npm start 下…

JOptionPane

JOptionPane类 1、属于javax.swing 包。 2、功能&#xff1a;定制四种不同种类的标准对话框。 ConfirmDialog 确认对话框。提出问题&#xff0c;然后由用户自己来确认&#xff08;按"Yes"或"No"按钮&#xff09; InputDialog 提示输入文本 MessageDialog …

react 错误边界_React with GraphQL和错误边界中的自定义错误页面

react 错误边界by Abi Noda通过Abi Noda React with GraphQL和错误边界中的自定义错误页面 (Custom error pages in React with GraphQL and Error Boundaries) If you like this article please support me by checking out Pull Reminders, a Slack bot that sends your tea…

UITextField 限制用户输入小数点后位数的方法

UITextField 限制用户输入小数点后位数的方法 位数限制: limited 在UITextField的代理方法中添加类似如下代码 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {NSMutableString * futureStr…

[BZOJ 1002] [FJOI 2007] 轮状病毒

1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3045 Solved: 1687[Submit][Status][Discuss]Description 给定n(N<100)&#xff0c;编程计算有多少个不同的n轮状病毒。 Input 第一行有1个正整数n。 Output 将编程计算出的不同的n轮状病毒数输出…

微信小程序左上角返回按钮跳转到指定页面

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 在当前页面的 onUnload 执行页面跳转 onUnload: function () {wx.reLaunch({url: ../logs/logs})}, 代码讲解&#xff1a;监听页面卸载的函数&#xff0c;把页面重定向到指定的 页面&#xff1b;

过度沉思_从沉思到演出:我如何开始我的自由职业

过度沉思by Ashley MacWhirter作者&#xff1a;Ashley MacWhirter 从沉思到演出&#xff1a;我如何开始我的自由职业 (From grit to gigs: how I started my freelancing business) In less than one year, I went from a Georgia Tech Coding Bootcamp graduate to a busines…

拾人牙慧篇之———QQ微信的第三方登录实现

一、写在前面 关于qq微信登录的原理之流我就不一一赘述了&#xff0c;对应的官网都有&#xff0c;在这里主要是展示我是怎么实现出来的&#xff0c;看了好几个博客&#xff0c;有的是直接复制官网的&#xff0c;有的不知道为什么实现不了。我只能保证我的这个是我实现后才贴出来…

win7旗舰版下配置IIS服务器

选择上述的插件后&#xff0c;Windows 需要更新一段时间&#xff0c;并重启电脑 测试是否安装成功&#xff1a;http://localhost 注意&#xff1a;默认端口号是 80&#xff0c;不能和tomcat 的 80 端口同时重启 常见问题&#xff1a; 1.默认页面或者新添加的网站一直出现…

微信小程序 加载中 动画效果

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 效果图&#xff1a; 代码&#xff1a; <view class"line"><image src"../../img/line.png"></image></view>.line {height:1px;position:absolute;animat…

java开放源码_开放源码的第一周:我是如何参与的,以及我学到的东西

java开放源码by Chak Shun Yu泽顺宇 开放源码的第一周&#xff1a;我是如何参与的&#xff0c;以及我学到的东西 (My first week of open source: how I got involved, and what I’ve learned) When I started to write this post, I had finished my first serious week of …

几个不错的开源的.net界面控件

转自原文 几个不错的开源的.net界面控件 (转) 几个不错的开源的.net界面控件 - zt 介绍几个自己觉得不错的几个开源的.net界面控件&#xff0c;不知道是否有人介绍过。 DockPanel Suite&#xff1a;开发类似VS.NET的界面&#xff0c;#Develop就是使用的这个控件。 网址&#…

CSS 盒子模型(转)

CSS中&#xff0c; Box Model叫盒子模型&#xff08;或框模型&#xff09;&#xff0c;Box Model规定了元素框处理元素内容&#xff08;element content&#xff09;、内边距&#xff08;padding&#xff09;、边框&#xff08;border&#xff09; 和 外边距&#xff08;margin…

React 入门笔记 1

React的核心&#xff1a;JSX 首先&#xff0c;React 的页面就是js文件&#xff0c;看到这会有朋友疑惑了&#xff0c;js怎么写标签组件&#xff1f; 解&#xff1a;首先&#xff0c;我们需要了解JSX&#xff0c;什么是JSX&#xff1f; JSX就是JavaScript XML。一种在React组件…

自学成才翁_如何发挥自学成才的内在游戏

自学成才翁by Victor Cassone由Victor Cassone 如何发挥自学成才的内在游戏 (How to play the inner game of self-taught development) Teaching yourself software development is hard. Anyone who tells you otherwise most likely hasn’t done it before.自学软件开发非…

C语言回溯算法解决N皇后问题

回溯算法的模型是 x, not satisfy ? x-- : continue. 代码中x作列号&#xff0c;y[x]保存第x列上皇后放置的位置。 1 #include<stdio.h>2 #include<math.h>3 #define N 54 int position_check(int,int*);5 void print_board(int count,int* y);6 int main()7 {8 …

React 创建组件 使用组件 2

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 组件&#xff1a; 通过组件&#xff0c;可以将UI拆分成独立的&#xff0c;可重复使用的部分&#xff0c;从概念上讲&#xff0c;组件就像javaScript函数&#xff0c;它们接受任意输入&#xff08;称之为…

如何征服Webpack 4并构建一个出色的React应用

This article has been outdated with the new release for babel, kindly check the updated article “How to combine Webpack 4 and Babel 7 to create a fantastic React app”, last updated October 13th, 2018本文与babel的新版本已经过时&#xff0c;请查看更新的文章…

React State和生命周期 3

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 一&#xff1a;组件的生命周期 组件的生命周期可分成三个状态&#xff1a; 安装&#xff1a;已插入真实DOM更新&#xff1a;正在被重新渲染卸载&#xff1a;已移出真实DOM 生命周期的方法有&#xff1a…

TCP/IP 笔记 1.3 IP:网际协议

---恢复内容开始--- I P是T C P / I P协议族中最为核心的协议。所有的 T C P、U D P、I C M P及I G M P数据都以I P数据报格式传输。  不可靠( u n r e l i a b l e)的意思是它不能保证 I P数据报能成功地到达目的地。 I P仅提供最好的传输服务。如果发生某种错误时&#xff…

keras bi-lstm_LSTM用于文本生成的应用介绍-使用Keras和启用GPU的Kaggle Kernels

keras bi-lstmby Megan Risdal梅根里斯达尔(Megan Risdal) LSTM用于文本生成的应用介绍-使用Keras和启用GPU的Kaggle Kernels (An applied introduction to LSTMs for text generation — using Keras and GPU-enabled Kaggle Kernels) Kaggle recently gave data scientists …

201521123013 《Java程序设计》第13周学习总结

1. 本周学习总结 2. 书面作业 Q1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu.edu.cn&#xff0c;分析返回结果有何不同&#xff1f;为什么会有这样的不同&#xff1f; ping值不同&#xff08;time列&#xff09;&#xff0c;cec.jmu.edu.cn的ping值比较小。ping值&am…

React 事件 4

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 将参数传递给事件处理程序 在循环内部&#xff0c;通常需要将额外的参数传递给事件处理程序。例如&#xff0c;如果id是行ID&#xff0c;则以下任何一个都可以工作&#xff1a; <button onClick{(e…

border-radius

周知&#xff1a;OPPO R819T Android 4.2.1和红米某些机型上&#xff0c;webview中&#xff0c;如果一个元素定义了 border border-radius&#xff0c;这时如果该元素有背景&#xff0c;那么背景将会溢出圆角之外&#xff0c;Yo新增了一个方法来fix这个问题&#xff0c;大家之…

javascript调试_如何提高JavaScript调试技能

javascript调试Almost all software developers who have written even a few lines of code for the Web have had at least a quick glance at JavaScript. After all, it is currently one of the most in-demand programming languages.几乎所有甚至为Web编写了几行代码的软…

Java transient

原文出自&#xff1a;http://www.importnew.com/21517.html 1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口&#xff0c;这个对象就可以被序列化&#xff0c;java的这种序列化模式为开发者提供了很多便利&#xff0c;我们可以不必关系具体序列化的…

KBMMW 的日志管理器

kbmmw 4.82 最大的新特性就是增加了 日志管理器。 新的日志管理器实现了不同类型的日志、断言、异常处理、计时等功能。 首先。引用kbmMWLog.pas 单元后&#xff0c;系统就默认生成一个IkbmMWLog 实例&#xff1a; Log:IkbmMWLog; log 默认使用对应操作系统的日志功能。 为了能…

React 循环渲染 5

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 使用循环渲染的demo&#xff1a; const todoItems todos.map((todo) ><li key{todo.id}>{todo.text}</li> ); const todoItems todos.map((todo, index) >// Only do this if item…

面试时与人事交谈时间_如何与您的技术负责人交谈并解决通讯故障

面试时与人事交谈时间by Greg Sabo由格雷格萨博(Greg Sabo) 如何与您的技术负责人交谈并解决通讯故障 (How to talk to your tech lead and fix your communication glitches) Here’s where you messed up.这是你搞砸的地方。 Your tech lead told you to build out a new A…