GithubHelp home page GithubHelp logo

shuding / socket-chat-room Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 2.0 321 KB

TCP socket programming...only homework, overwhelmed by homework.

Home Page: http://quietshu.github.io/socket-chat-room

JavaScript 86.45% HTML 5.81% CSS 7.74%

socket-chat-room's Introduction

socket chat room

Binary & releases.

demo

Definition of the Protocol

Architecture

[server]
  <-- Wrapped TCP Socket -->
[client]
  <-- Wrapped IPC -->
[model/controller]
  <-- Angular.js -->
[view]

Types

  1. Request: client - request -> server - response -> client
  2. Push: server - push -> client

Data on Socket/TCP

{
  type
  token
  timestamp
  content
}

type

['push', 'res']

token

The token field of response should be equal to the original request which is a randomly generated integer.

content

Content field of messages.

"Segments"

JSON data -> stringify -> split to segments -> add No./total header -> binarize -> send

Demo

A request from the server (push) or client (request-response)

Stringify the message context:

{"type":"push","timestamp":1449345271889,"content":{"type":"msg","data":{"data":"It works.","name":"ds","time":1449345271886}}}

Split into segments:

{"type":"push","timestamp":1449
345271889,"content":{"type":"ms
g","data":{"data":"It works.","
name":"ds","time":1449345271886
}}}

Pre-encode "dots":

{"type":"push","timestamp":1449
345271889,"content":{"type":"ms
g","data":{"data":"It works(dot)","
name":"ds","time":1449345271886
}}}

Add header info:

.1/5.{"type":"push","timestamp":1449
.2/5.345271889,"content":{"type":"ms
.3/5.g","data":{"data":"It works(dot)","
.4/5.name":"ds","time":1449345271886
.5/5.}}}

Binarize and TCP transmit.

Client

Receive all segments and decode with UTF-8.

Parse header and push segments into a stack:

...
5/5 }}}
4/5 name":"ds","time":1449345271886
3/5 g","data":{"data":"It works(dot)","
2/5 345271889,"content":{"type":"ms
1/5 {"type":"push","timestamp":1449
...

Pop out the whole message and concat segments, decode dots:

{"type":"push","timestamp":1449345271889,"content":{"type":"msg","data":{"data":"It works.","name":"ds","time":1449345271886}}}

Image

In client we encode the image data with base64, and wrapped as the context of a message.

...{"data":"data:image/png;base64,iVB..."}...

For the receiver, just parse the base64 string and decode it.

Emoji

Send as text like :xxxxx:.

Build

Requirements

Babel & Electron & Enclose

  1. $ npm i babel -g
  2. $ npm i electron-prebuilt -g
  3. $ npm i enclose -g

Build Client

  1. $ cd client
  2. $ npm install
  3. $ npm run build
  4. $ npm start

Run Server

  1. $ cd server
  2. $ npm install
  3. $ npm start

Pack Client App

OS X (for darwin and x64):

  1. $ cd client
  2. $ npm run pack

Other platforms:

modify pack command in client/package.json, see electron-packager for more details.

Acknowledgements

License

Shu Ding [email protected].

The MIT license.

socket-chat-room's People

Contributors

shuding avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

socket-chat-room's Issues

Cannot find module 'ipc'

很神奇地来提issue... :)

使用master的最新commit, 根据文档完成操作,client server 各自 npm start 之后,前端登录界面一闪而过,进入聊天界面,所有操作不可用。查看chrome 发现如下错误:
2016-05-25 20 49 04

查看 electron 文档,发现 ipc 模块应该是 electron 内置的,所以不清楚这边是怎么回事...

使用 最新 release 的 .app 文件的话,一切是正常的。

ps: 按照本repo的文档使用 npm i electron -g 的话, cannot find command 'electron',遂去 electron 的 repo 使用 npm install electron-prebuilt -g,不知道是否影响了 ipc。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.