-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat-node.js
110 lines (101 loc) · 2.91 KB
/
chat-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const lotion = require('lotion');
const lotionPort = 3010;
const socketPort = 8081;
const {peers} = require('./config.js');
const socket = require('socket.io');
const express = require('express');
const axios = require('axios');
async function main() {
console.log('starting a blockchain interface on port ' + lotionPort + '...\n');
console.log(
`
chain state : http://localhost:${lotionPort}/state
transactions: http://localhost:${lotionPort}/txs
`
)
let opts = {
peers : peers,
genesis: './genesis.json',
keys: './priv_validator.json',
p2pPort: 46656,
tendermintPort: 46657,
logTendermint: true,
createEmptyBlocks: false,
initialState: {
messages: [{
sender: 'Markus',
message: 'welcome to tendermint '
},
{
sender: 'Markus',
message: 'on the blockchain'
}
]
}
};
// * Create a new instance of Lotion
let app = lotion(opts);
let msgHandler = (state, tx, chainInfo) => {
if (
typeof tx.sender === 'string' &&
typeof tx.message === 'string' &&
tx.message.length <= 1000
) {
if (tx.message !== '') {
state.messages.push({
sender: tx.sender,
message: tx.message
});
}
}
}
app.use(msgHandler);
// Start the Lotion app
const server = app.listen(lotionPort).then(genesis => {
console.log(genesis);
}, err => {
console.log(err);
})
const socketServer = express().listen(socketPort)
const io = socket(socketServer, {
origins: '*:*'
});
lastMessagesLength = 0
async function updateState() {
let {
data
} = await axios.get('http://localhost:' + lotionPort + '/state')
let messages = await data.messages
if (messages !== undefined && messages.length > lastMessagesLength) {
let newMessages = messages.slice(lastMessagesLength, messages.length)
io.sockets.emit('chat', newMessages)
lastMessagesLength = messages.length
}
}
io.on('connection', async (socket) => {
console.log('made socket connection', socket.id);
// on new socket connection emmit all messages
try {
let {
data
} = await axios.get('http://localhost:' + lotionPort + '/state')
let messages = await data.messages
io.sockets.connected[socket.id].emit('chat', messages)
} catch (e) {
console.log('Error caught:' + e);
}
//setting Interval to fetch data from local host and send it when new message occurs
let interval = setInterval(() => {
updateState()
}, 50)
socket.on('disconnect', function() {
clearTimeout(interval)
console.log('user disconnected' + 'total number of users: ' + Object.keys(io.sockets.connected).length);
})
})
}
process.on('unhandledRejection', function(reason, p) {
console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
console.trace();
});
main()