3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

なんちゃって slack clone

Last updated at Posted at 2020-05-17

概要

slack cloneを作ってみた。

出来る事

  • リアルタイム チャット

できないこと

  • 認証
  • チャンネル
  • ダイレクトメッセージ
  • スレッド
  • ワークスペース
  • データの永続化

写真

image.png

利用したライブラリー、サービス

フロントエンド

  • plunkerに設置
  • フレームワークは、vue
  • UIは、tailwind

通信

  • websocket

バックエンド

  • node-red
  • enebularでedit
  • herokuに設置

flow

image.png

サンプルコード


var url = "wss://.herokuapp.com/pub";
var ws = null;
var app = new Vue({
	el: '#app',
	data: function() {
		return {
			id: "",
			message: "",
			messages: [{
				"msg": "message",
				"id": "123",
				"time": "11:46"
			}]
		}
	},
	methods: {
		onMessage: function(e) {
			var json = JSON.parse(e.data);
			this.messages.unshift({
				"msg": json.msg,
				"id": json.id,
				"time": this.getTime()
			});
		},
		onError: function(e) {
			alert("接続に失敗しました。やり直してください。");
		},
		onClose: function(e) {
			//alert("close");
			ws = null;
			setTimeout("app.open()", 3000);
		},
		open: function() {
			if (ws == null)
			{
				ws = new WebSocket(url);
				ws.onopen = this.onOpen;
				ws.onmessage = this.onMessage;
				ws.onclose = this.onClose;
				ws.onerror = this.onError;
			}
		},
		onOpen: function(e) {
			//alert("接続しました。");
		},
		send: function(msg) {
			ws.send(JSON.stringify({
				'msg': msg,
				'id': this.id
			}));
		},
		getUniq: function() {
		  var strong = 10000;
  		return Math.floor(strong * Math.random()).toString(16) 
		},
		keypress: function() {
			if (this.message == "")
			{
				return;
			}
			this.send(this.message);
			this.message = "";
		},
		getTime: function() {
			return new Date().toTimeString();
		}
	},
	mounted: function() {
		this.id = this.getUniq();
		this.open();
	}
});




以上。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?