All Projects → spiritree → Vue Socket.io Chat

spiritree / Vue Socket.io Chat

Licence: mit
💬 TypeScript + Vue + Express/Koa + Socket.io

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Vue Socket.io Chat

Trafficlight
🚦 Flexible NodeJS Routing Decorators for API Routing
Stars: ✭ 69 (+13.11%)
Mutual labels:  express, koa, koa2
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (+1037.7%)
Mutual labels:  express, koa, koa2
Blog Service
blog service @nestjs
Stars: ✭ 188 (+208.2%)
Mutual labels:  express, koa, koa2
Vue Chat
👥Vue全家桶+Socket.io+Express/Koa2打造一个智能聊天室。
Stars: ✭ 887 (+1354.1%)
Mutual labels:  express, koa, koa2
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+17878.69%)
Mutual labels:  express, koa, koa2
Github Ranking
🔍GitHub不同语言热门项目排行,Vue.js做页面展示
Stars: ✭ 160 (+162.3%)
Mutual labels:  express, koa, koa2
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+208.2%)
Mutual labels:  express, koa, koa2
Node Typescript Koa Rest
REST API boilerplate using NodeJS and KOA2, typescript. Logging and JWT as middlewares. TypeORM with class-validator, SQL CRUD. Docker included. Swagger docs, actions CI and valuable README
Stars: ✭ 739 (+1111.48%)
Mutual labels:  koa, koa2
Koa Passport
Passport middleware for Koa
Stars: ✭ 748 (+1126.23%)
Mutual labels:  koa, koa2
Koa Weixin Jssdk
koa weixin jssdk middleware
Stars: ✭ 7 (-88.52%)
Mutual labels:  koa, koa2
Cykspace Node
博客后台服务~~ 👉👉 http://www.cykspace.com
Stars: ✭ 23 (-62.3%)
Mutual labels:  koa, koa2
Vue Koa Demo
🔰A simple full stack demo(CSR & SSR & Docker Support) written by Vue2 & Koa2(Koa1 verson also completed)
Stars: ✭ 730 (+1096.72%)
Mutual labels:  koa, koa2
Koa Helmet
Important security headers for koa
Stars: ✭ 595 (+875.41%)
Mutual labels:  koa, koa2
Nodeclub Koa
use koa to rewrite nodeclub
Stars: ✭ 18 (-70.49%)
Mutual labels:  koa, koa2
Kona
a node.js service framework built on koa.js (generators)
Stars: ✭ 23 (-62.3%)
Mutual labels:  express, koa
Cabin
🌲 Cabin is the best JavaScript and Node.js logging service and logging npm package
Stars: ✭ 622 (+919.67%)
Mutual labels:  express, koa
Koa Dec Router
An ES6 decorator + class based router, support inherit, override, priority, auto load controllers, etc.
Stars: ✭ 19 (-68.85%)
Mutual labels:  koa, koa2
Deprecated
🚀 Framework for building universal web app and static website in Vue.js (beta)
Stars: ✭ 858 (+1306.56%)
Mutual labels:  express, koa
Mysrv
Yet another Node.js web framework, based on koa.js 又一个 Node.js MVC 框架,基于Koa2
Stars: ✭ 10 (-83.61%)
Mutual labels:  koa, koa2
Vue Qq
🎨 Vue family bucket with socket.io and express/koa2 , create a web version of mobile QQ, supporting real-time group chat, real-time private chat, special care, shielding chat, smart IP geographic location, real-time display temperature and other QQ core functions
Stars: ✭ 861 (+1311.48%)
Mutual labels:  express, koa2

教你用Vue渐进式搭建聊天室,从JavaScript=>TypeScript

Build Status GitHub license

English Document

前言

Vue+Socket.io这个轮子已经有很多人造过了,为了不重复造轮子,我将本项目以三阶段实现(大家可以在github中的Releases查看):

  • 纯前端(Vuex)
  • 后端+前端(JavaScript)
  • 后端+前端(TypeScript)

希望能给大家一个渐进学习的经验。

预览地址:https://app.spiritree.me/

技术栈

Vue + Webpack + TypeScript + Express + SCSS + Socket.io + Gulp

Vue-cli创建工程
`npm install -g vue-cli`

vue init webpack my-project

vue init ElemeFE/webpack-typescript my-project(感谢饿了么分享的TypeScript的模板)

这样就在当前目录下创建了完整的工程模板


Socket.io
在Server端(Express)
import * as socketIo from 'socket.io'

this.io.on('connection', (socket: any) => {
  socket.on('sendMessage', (data: any) => {
    this.io.emit('boardcastMessage', data)
})

在Client端(Vue)

<script lang="ts">
/// <reference path="../../socket.io.d.ts" />
export default Vue.extend({
  mounted() {
    socket.on('boardcastMessage', (data: any) => {
        this.$store.dispatch('sendMessage', { data })
    })
  }
})

Server端常用API:

socket.emit():向建立该连接的客户端发送消息

socket.on():监听客户端发送信息

io.sockets.emit():向所有客户端广播

Client端常用API:

socket.emit():向服务端发送消息

socket.on():监听服务端发来的信息


TypeScript

关于TypeScript的基本知识,可以直接看xcatliu整理的教程,简单易懂,有Java/C#基础就可快速上手。 TypeScript 入门教程

webpack+TypeScript(前端)

Vue + TypeScript 尝鲜体验

也可用官方插件vue-class-component

本项目参考 vue init ElemeFE/webpack-typescript my-project

先添加声明文件(Vue全家桶自带就不需要了) 本项目用到Express和Socket.io

npm install typescript --save-dev

npm i ts-loader --save-dev

在webpack.base.conf.js中添加

{
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      },
    ],
  }
}

在根目录添加声明文件

socket.io.d.ts(为了编译通过)

declare namespace socket {
  var on: any;
  var emit: any;
  var data: any;
}

在每个Vue文件中添加

vuechat2.png


Gulp+TypeScript(后端)

npm install gulp --save-dev

npm install gulp-typescript --save-dev

npm install @types/express --save-dev

npm install @types/socket.io --save-dev

Server文件夹结构

├── app.js
├── gulpfile.js
├── register.js
├── src
│   ├── type-app.ts
│   └── type-register.ts
├── tsconfig.json
├── type-app.js
└── type-register.js

添加tsconfig.json

TypeScript官方手册

{
  "include": [
    "src/*.ts"
  ],
  "compilerOptions": {
    "noImplicitAny": true,
    "lib": ["es6"],
    "target": "es5",
    "outDir": ""
  }
}

配置gulpfile.js

var gulp = require("gulp");
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");

gulp.task("build", function () {
    return tsProject.src()
        .pipe(tsProject())
        .js.pipe(gulp.dest(""));
});

从JavaScript=>TypeScript

vuechat3.png


部署

Linux+Nginx的组合,可以一键部署虚拟主机 OneinStack

部署的遇到的坑 https://github.com/socketio/socket.io/issues/1942

Error during WebSocket handshake: Unexpected response code: 400

在nginx.conf添加

location / {
	proxy_pass http://localhost:8989;
	proxy_http_version 1.1;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "upgrade";
	proxy_set_header Host $host;
 }

如何使用

预览地址:https://app.spiritree.me/

Github地址:https://github.com/spiritree/vue-socket.io-chat

开启JavaScript服务端

# 从Github克隆本项目
`git clone https://github.com/spiritree/vue-socket.io-chat.git`

# 安装依赖
`npm install`

# 开启本地服务器
`npm run server`

开启TypeScript服务器

# 从Github克隆本项目
`git clone https://github.com/spiritree/vue-socket.io-chat.git`

# 安装依赖
`npm install`

# 切换目录
`cd server`

# 将TypeScript编译成JavaScript
`gulp build`

# 开启本地服务器
`npm run tsserver`

浏览器访问 http://localhost:8989 如遇在线列表不同步,刷新重进即可

预览

vuechat0.png vuechat1.png

参考资料

LICENSE

MIT

Copyright (c) 2017-present, spiritree

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].