websockets

Creating a very primitive chat app in SwiftUI, while using Swift and WebSockets to create the chat server. It’s Swift from top to bottom, bay-bee!

在SwiftUI中创建一个非常原始的聊天应用程序,同时使用Swift和WebSockets创建聊天服务器。 从上到下都是迅捷的,海湾蜂!

This tutorial is also available in Markdown, together with the final code, on Github.

Markdown中还提供了本教程,以及Github上的最终代码。

介绍 (Introduction)

In this tutorial we’ll make a rather primitive, but functional chat app. The app will run on iOS or macOS — or both! The beauty of SwiftUI is how little effort it takes to make a multiplatform app.

在本教程中,我们将制作一个相当原始但实用的聊天应用程序。 该应用程序将在iOS或macOS上运行-或同时在两者上运行! SwiftUI的优点在于制作一个多平台应用程序需要花费很少的精力。

Of course, a chat app will have very little use without a server to talk to. Hence we’ll be making a very primitive chat server as well, utilizing WebSockets. Everything will be built in Swift and run locally on your machine.

当然,如果没有服务器可与之聊天,则聊天应用将很少使用。 因此,我们还将利用WebSockets制造一个非常原始的聊天服务器。 一切都将在Swift中构建,并在您的计算机上本地运行。

This tutorial assumes you already have a bit of experience developing iOS/macOS apps using SwiftUI. Although concepts will be explained as we go, not everything will be covered in depth. Needless to say, if you type along and follow the steps, by the end of this tutorial you’ll have a working chat app (for iOS and/or macOS), that communicates with a server that you also made! You will also have a basic understanding of concepts like server-side Swift and WebSockets.

本教程假定您已经具有使用SwiftUI开发iOS / macOS应用程序的经验。 尽管我们将在本文中解释概念,但并不是所有内容都会深入介绍。 不用说,如果您键入并按照步骤进行操作,那么到本教程结束时,您将拥有一个可正常使用的聊天应用程序(适用于iOS和/或macOS),可与您同时制造的服务器通信! 您还将对服务器端Swift和WebSockets等概念有基本的了解。

If none of that interests you, you can always just download the final code from Github.

如果您对这些都不感兴趣,可以随时从Github下载最终代码 。

快速摘要 (Quick summary of what’s to come)

In short, we will start by making a very simple, plain, featureless server. We’ll build the server as a Swift Package, then add the Vapor web framework as a dependency. This will help us setup a WebSocket server with just a few lines of code.

简而言之,我们将从制作一个非常简单,简单,无功能的服务器开始。 我们将服务器构建为Swift软件包,然后将Vapor Web框架添加为依赖项。 这将帮助我们仅用几行代码来设置WebSocket服务器。

Afterwards we will start building the frontend chat app. Quickly starting with the basics, then adding features (and necessities) one by one.

之后,我们将开始构建前端聊天应用程序。 快速从基础开始,然后逐一添加功能(和必需品)。

Most of our time will be spent working on the app, but we’ll be going back and forth between the server code and the app code as we add new features.

我们大部分时间将花在开发应用程序上,但是随着我们添加新功能,我们将在服务器代码和应用程​​序代码之间来回切换。

要求 (Requirements)

  • macOS 10.15+macOS 10.15以上
  • Xcode 12 beta 5+Xcode 12 Beta 5+

Optional

可选的

  • macOS 11 beta

    macOS 11测试版

    (if you want to run the app on macOS)

    (如果您想在macOS上运行该应用程序)

  • iPhone/iPad running iOS 14 beta 5+

    运行iOS 14 Beta 5+的iPhone / iPad

    (if you want to run the app on a physical device)

    (如果您想在物理设备上运行该应用程序)

Let’s begin!

让我们开始!

创建服务器 (Creating the server)

Open Xcode 12 and start a new project (File > New Project). Under Multiplatform select Swift Package.

打开Xcode 12并启动一个新项目( File> New Project )。 在Multiplatform下,选择Swift Package

Call the Package something logical — something self explanatory — like “ChatServer”. Then save it wherever you like.

将该软件包称为逻辑的(可以自我解释的),例如“ ChatServer ”。 然后将其保存在您喜欢的任何位置。

Swift Package?

迅捷套餐?

When creating a framework or multiplatform software (e.g. for macOS and Linux) in Swift, Swift Packages are the preferred way to go. They’re the official solution for creating modular code that other Swift projects can easily use. A Swift Package doesn’t necessarily have to be a modular project though: it can also be a stand-alone executable that simply uses other Swift Packages as dependencies (which is what we’re doing).

在Swift中创建框架或多平台软件(例如,用于macOS和Linux)时,Swift软件包是首选的方式。 它们是创建其他Swift项目可以轻松使用的模块化代码的官方解决方案。 但是,Swift软件包不一定必须是模块化项目:它也可以是独立的可执行文件,它仅将其他Swift软件包用作依赖项(这就是我们正在做的事情)。

It may have occurred to you that there’s no Xcode project (.xcodeproj) present for the Swift Package. To open a Swift Package in Xcode like any other project, simply open the Package.swift file. Xcode should recognize you're opening a Swift Package and opens the entire project structure. It will automatically fetch all the dependencies at the start.

您可能已经想到,Swift软件包不存在Xcode项目( .xcodeproj )。 要像其他任何项目一样在Xcode中打开Swift包,只需打开Package.swift文件。 Xcode应该识别出您正在打开Swift软件包并打开了整个项目结构。 它会在开始时自动获取所有依赖项。

You can read more about Swift Packages and Swift Package Manager on the official Swift website.

您可以在Swift的官方网站上阅读有关Swift Packages和Swift Package Manager的更多信息。

设置Package.swift (Setup Package.swift)

To handle all the heavy lifting of setting up a server, we’ll be using the Vapor web framework. Vapor comes with all the necessary features to create a WebSocket server.

为了处理设置服务器的所有繁重工作,我们将使用Vapor Web框架 。 蒸气具有创建WebSocket服务器所需的所有必要功能。

WebSockets?

WebSockets?

To provide the web with the ability to communicate with a server in realtime, WebSockets were created. It’s a well described spec for safe realtime (low-bandwidth) communication between a client and a server. E.g.: multiplayer games and chat apps. Those addictive in-browser multiplayer games you’ve been playing on valuable company time? Yup, WebSockets!

为了使Web能够与服务器实时通信,创建了WebSocket。 这是对客户端和服务器之间的安全实时(低带宽)通信的规范描述。 例如:多人游戏和聊天应用程序。 您在宝贵的公司时间里玩的那些 令人上瘾 的浏览器内 多人 游戏 ? 是的,WebSockets!

However, if you wish to do something like realtime video streaming you’re best looking for a different solution.

websockets_一个简单的聊天应用,带有swiftui和websockets或背面swift正面相关推荐

  1. Android(安卓)一个简单的聊天界面的实现(eclipse实现)

    这几天刚刚学习一下安卓的编程,尝试制作了一个简单的聊天界面(还没有实现网络等后续功能)软件界面如图.(使用eclipse实现) 当输入一些内容后,聊天界面可以下拉显示更多的聊天信息,如下图 首先对这个 ...

  2. 通过python 构建一个简单的聊天服务器

    构建一个 Python 聊天服务器 一个简单的聊天服务器 现在您已经了解了 Python 中基本的网络 API:接下来可以在一个简单的应用程序中应用这些知识了.在本节中,将构建一个简单的聊天服务器.使 ...

  3. 通信软件基础B-重庆邮电大学-Java-编程实现一个简单的聊天程序-多线程编程实现

    实验任务六 编程实现一个简单的聊天程序-多线程编程实现 1. 系统设计要求 编程实现一个简单的聊天程序,实现两台计算机间的信息交互,使用多线程编程实现:可同时连接多个客户端,服务器收到客户端发送的消息 ...

  4. WebSocket(二) -- 使用原生webSocket实现一个简单的聊天

    上文中,我们已经基本了解了webscoket的原理以及部分api的实现,接下来我们就使用websocket来实现一个简单的聊天室功能. 1. 需求分析 1.1 登陆聊天室: 1.2 登陆成功后与别人进 ...

  5. 用ServletContext做一个简单的聊天室

    这里主要是ServletContext的一个特性:ServletContext是一个公共的空间,可以被所有的客户访问.由此可见ServletContext比cookie和session的作用范围要大[ ...

  6. Netty - 一个简单的聊天室小项目

     经过一段时间对Netty的学习,我们对Netty各版本以及像ProtocolBuffers等技术应用都有了不少相关的了解, 我们就用这段时间学到的只是做一个简单的聊天室的小项目来练习自己学到的技术. ...

  7. python开发一个简单的聊天室

    使用python的twisted框架编写一个简单的聊天室 下面是基本架构 基本架构图 -- coding:utf-8 -- from twisted.internet.protocol import ...

  8. C语言能干什么?手把手教你写一个简单的聊天软件

    一.服务端代码 因为端口号容易被占用的原因,所以IP地址和端口号采用参数传递的方法,即 int main(int argc,char **argv) 1.头文件 #include <stdio. ...

  9. TensorFlow应用:制作一个简单的聊天机器人

    现在很多卖货公司都使用聊天机器人充当客服人员,许多科技巨头也纷纷推出各自的聊天助手,如苹果Siri.Google Now.Amazon Alexa.微软小冰等等.前不久有一个视频比较了Google N ...

最新文章

  1. 深入卷积神经网络背后的数学原理 | 技术头条
  2. 组合恒等式7 组合变换的互逆公式 简介与简单例子
  3. golang函数多值返回示例
  4. 如何在linux系统下对文件夹名有空格的文件
  5. 【招聘(深圳)】敢为软件技术有限公司 .Net 工程师
  6. restful解决什么问题_当您陷入RESTful,WordPress和一个困难的地方时,如何解决CMS问题...
  7. 数据:哈佛大学新生近五成是富二代!
  8. 华为手机8.0.0怎么找到云相册_华为G9怎么找到云相册_失而复得 华为“查找手机”功能有多强大?快来了解一下......
  9. 面试官系统精讲Java源码及大厂真题 - 24 举一反三:队列在 Java 其它源码中的应用
  10. web测试抓包基本功——使用Google的F12
  11. 边缘和核心交换——应用层CS、P2P、混合模式
  12. 沃趣赵晨 | 从技术岗位到产品经理:漫谈IT产品经理的生存之道
  13. C++ VS项目属性的一些配置项的总结
  14. C++实现前向欧拉法Forward Euler解决偏微分方程
  15. 使用74LS160设计六进制计数器
  16. js网页繁体简体转换
  17. WIFI基础入门--802.11k--无线局域网络频谱测量
  18. Android开发之漫漫长途 XII——Fragment 详解
  19. 心理学与计算机交叉学,认知心理学其与邻近学科交叉产物
  20. 【图像融合学习笔记001】图像融合论文及代码网址整理总结(1)——多聚焦图像融合

热门文章

  1. 蓝桥杯题解-高僧斗法
  2. 天翎数字孪生解决方案
  3. Oracle 数据库自动诊断库 ADR(Automatic Diagnostic Repository)简介 发表在 数据和云
  4. vivos11t刷机包 android,步步高vivo S11t一键救砖教程,轻松刷回官方系统
  5. python科学计算够用吗_为何选用python进行科学计算
  6. linux无法打开某些网页,Linux无法打开网页
  7. UE4实现多国语言翻译
  8. 浅谈消防设备电源监控系统的设计与应用
  9. 迅为龙芯2K1000开发板国产处理器操作系统
  10. 数据结构第二版(朱昌杰版)补第一章习题答案