32

[练手项目]Gin+websocket 的多人聊天室

 3 years ago
source link: https://studygolang.com/articles/31089
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

go-gin-chat(Gin+websocket 的多人聊天室)

练手小项目,为熟悉Gin框架跟websocket使用 :yellow_heart::yellow_heart::yellow_heart::yellow_heart::yellow_heart::yellow_heart:

在线demo (PS: 请尽量使用Chrome游览器)

github地址

结构

.
|-- conf
|-- controller
|-- models
|-- routes
|-- services
|   |               `-- img_kr
|   |               `-- message_service
|   |               `-- session
|   |               `-- user_service
|                   `-- validator
|-- sql
|-- static
|   |               `-- bootstrap
|   |   |           `-- css
|   |   |           `-- fonts
|   |               `-- js
|   |-- emoji
|   |-- images
|   |   |           `-- face
|   |   |           `-- rooms
|   |   |           `-- theme
|   |               `-- user
|   |-- javascripts
|   |-- rolling
|   |   |           `-- css
|   |               `-- js
|   `-- stylesheets
|-- tmp
|-- tmp_images
|-- views
`-- ws

界面

YBZnEvq.png!mobileV7B36vM.png!mobileNnAbuq.png!mobileN3ERnan.png!mobile

feature

  • 登录/注册(防止重复登录)
  • 群聊(支持文字、emoji、图片)
  • 历史消息查看(暂时仅支持最新100条)
  • go mod 包管理
  • 静态资源嵌入,运行只依赖编译好的可执行文件与mysql

database

mysql

aUJraeZ.png!mobile

Tools

  • 模板提供
  • github.com/gin-gonic/gin
  • gorm.io/driver/mysql
  • gorm.io/gorm
  • github.com/gravityblast/fresh
  • github.com/valyala/fasthttp
  • github.com/spf13/viper
  • https://blog.hi917.com/detail/87.html

使用

# 自行导入数据库文件 sql/go_gin_chat.sql
git clone github.com/hezhizheng/go-gin-chat
cd go-gin-chat
cp conf/config.go.env config.go // 根据实际情况修改配置
go-bindata -o=bindata/bindata.go -pkg=bindata ./static/... ./views/... // 安装请参考 https://blog.hi917.com/detail/87.html
go run main.go

nginx 部署

server {
    listen 80;
    server_name  go-gin-chat.hzz.cool;

    location ~ .*\.(gif|jpg|png|css|js)(.*) {
                proxy_pass http://127.0.0.1:8322;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_cache cache_one;
                proxy_cache_valid 200 302 24h;
                proxy_cache_valid 301 30d;
                proxy_cache_valid any 5m;
                expires 90d;
                add_header wall  "Big brother is watching you";
    }

   location / {
       try_files /_not_exists_ @backend;
   }

   location @backend {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            $http_host;

        proxy_pass http://127.0.0.1:8322;
    }

   location /ws {
        proxy_pass http://127.0.0.1:8322;
        proxy_redirect off;
        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
        proxy_read_timeout 6000s;
   }

Tip

go-bindata -o=bindata/bindata.go -pkg=bindata ./static/... ./views/...

todo

  • [ ] 心跳机制
  • [ ] 多频道聊天
  • [ ] 私聊
  • [ ] 在线用户列表
  • [ ] https支持

Recommend

  • 73

    本文仅对一些关键性的知识点进行解释,具体请Fork源码学习。 Demo页面如果没啥人的话可以自己新建几个页面复制地址进入,每个页面都是一个独立的访客,兼容PC和移动端访问。 Demo 演示 Github源码 博客原文 前端实现 前端技术栈 Parcel:构建

  • 36
    • blog.51cto.com 6 years ago
    • Cache

    搭建Websocket简易聊天室-11950887

    本文,我们通过Egret和Node.js实现一个在线聊天室的demo。主要包括:聊天,改用户名,查看其他用户在线状态的功能。大致流程为,用户访问网页,即进入聊天状态,成为新游客,通过底部的输入框,可以输入自己想说的话,点击发布,信息呈现给所有在聊天的人的页面。...

  • 59
    • segmentfault.com 5 years ago
    • Cache

    用gorilla websocket 搞一个聊天室

    这个demo实现了: 消息广播 心跳检测 通过命令行来进行聊天 具体逻辑都在 websocket.go 这个文件里 这里的核心就是 aliveList 这个全局变量, 负责把消息分...

  • 61
    • 掘金 juejin.im 5 years ago
    • Cache

    微信小程序websocket聊天室

    背景 最近做了一个微信小程序的即时通讯功能,之前我也做过node.js的websocket服务,不过是在web端应用的socket.io服务。小程序本身对http、websocket等连接均有诸多限制,所以这次项目选择了node.js自带的ws模块。 服务端

  • 40

    websocket是一种基于tcp的新网络协议,它实现了浏览器和服务器之间的双全工通信,允许服务端直接向客户端发送数据

  • 16

    Websocket直播间聊天室教程 - GoEasy快速实现聊天室 最近两年直播那个火啊,真的是无法形容!经常有朋友问起,我想实现一个直播间聊天或者我想开发一个聊天室, 要如何开始呢?

  • 6

    使用websocket,bootstrap的在线聊天室 – Mike's Blog跳至内容 用jQuery,Bootstrap,Javascript,...

  • 12
    • www.cnblogs.com 3 years ago
    • Cache

    Vue开发多人聊天室 复盘总结

    在上个月初,接到一个需求,要开发一个 聊天通讯 模块 并且 集成到 项目中的多个 入口,实现业务数据的记录追踪. 接到需求后,还挺开心,这是我第一次 搞 通讯 类的需求,之前一直是 B 端 的业务需...

  • 9

    宝塔面板+Fiora安装Web在线多人匿名聊天室教程 2021-11-1715:14:38

  • 13

    springboot+freemark+websocket+MySQL实现的Java web在线聊天系统,主要实现的功能有: 1、用户注册、登录。 2、搜索用户添加好友。 3、查看好友申请列表,同意或拒绝好友请求。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK