Skip to content

Commit

Permalink
Merge pull request #50 from stakwork/multi-socket
Browse files Browse the repository at this point in the history
Multi socket
  • Loading branch information
Evanfeenstra authored Jun 15, 2020
2 parents 635de71 + 2620f6a commit b93b37d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 43 deletions.
13 changes: 9 additions & 4 deletions api/controllers/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const sendAttachmentMessage = async (req, res) => {
file_name,
ttl,
price, // IF AMOUNT>0 THEN do NOT sign or send receipt
reply_uuid,
} = req.body

console.log('[send attachment]', req.body)
Expand Down Expand Up @@ -91,7 +92,7 @@ const sendAttachmentMessage = async (req, res) => {
const remoteMessageContent = remote_text_map?JSON.stringify(remote_text_map) : remote_text

const uuid = short.generate()
const message = await models.Message.create({
const mm:{[k:string]:any} = {
chatId: chat.id,
uuid: uuid,
sender: owner.id,
Expand All @@ -106,7 +107,9 @@ const sendAttachmentMessage = async (req, res) => {
date,
createdAt: date,
updatedAt: date
})
}
if(reply_uuid) mm.replyUuid=reply_uuid
const message = await models.Message.create(mm)

console.log('saved attachment msg from me',message.id)

Expand All @@ -125,6 +128,7 @@ const sendAttachmentMessage = async (req, res) => {
mediaKey: media_key_map,
mediaType: mediaType,
}
if(reply_uuid) msg.replyUuid=reply_uuid
network.sendMessage({
chat: chat,
sender: owner,
Expand Down Expand Up @@ -389,7 +393,7 @@ const receivePurchaseAccept = async (payload) => {

const receivePurchaseDeny = async (payload) => {
console.log('=> receivePurchaseDeny')
var date = new Date();
var date = new Date()
date.setMilliseconds(0)
const {owner, sender, chat, amount, mediaToken} = await helpers.parseReceiveParams(payload)
if(!owner || !sender || !chat) {
Expand Down Expand Up @@ -420,7 +424,7 @@ const receiveAttachment = async (payload) => {
var date = new Date();
date.setMilliseconds(0)

const {owner, sender, chat, mediaToken, mediaKey, mediaType, content, msg_id, chat_type, sender_alias, msg_uuid} = await helpers.parseReceiveParams(payload)
const {owner, sender, chat, mediaToken, mediaKey, mediaType, content, msg_id, chat_type, sender_alias, msg_uuid, reply_uuid} = await helpers.parseReceiveParams(payload)
if(!owner || !sender || !chat) {
return console.log('=> no group chat!')
}
Expand All @@ -438,6 +442,7 @@ const receiveAttachment = async (payload) => {
if(mediaToken) msg.mediaToken = mediaToken
if(mediaKey) msg.mediaKey = mediaKey
if(mediaType) msg.mediaType = mediaType
if(reply_uuid) msg.replyUuid = reply_uuid
const isTribe = chat_type===constants.chat_types.tribe
if(isTribe) {
msg.senderAlias = sender_alias
Expand Down
8 changes: 6 additions & 2 deletions api/controllers/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const sendPayment = async (req, res) => {
dimensions,
remote_text_map,
contact_ids,
reply_uuid,
} = req.body

console.log('[send payment]', req.body)
Expand Down Expand Up @@ -69,6 +70,7 @@ const sendPayment = async (req, res) => {
}
if(text) msg.messageContent = text
if(remote_text) msg.remoteMessageContent = remote_text
if(reply_uuid) msg.replyUuid=reply_uuid

if(muid){
const myMediaToken = await tokenFromTerms({
Expand All @@ -92,6 +94,7 @@ const sendPayment = async (req, res) => {
msgToSend.mediaTerms = {muid,meta:{dim:dimensions}}
}
if(remote_text) msgToSend.content = remote_text
if(reply_uuid) msgToSend.replyUuid=reply_uuid

// if contact_ids, replace that in "chat" below
// if remote text map, put that in
Expand Down Expand Up @@ -128,7 +131,7 @@ const receivePayment = async (payload) => {
var date = new Date();
date.setMilliseconds(0)

const {owner, sender, chat, amount, content, mediaType, mediaToken, chat_type, sender_alias, msg_uuid} = await helpers.parseReceiveParams(payload)
const {owner, sender, chat, amount, content, mediaType, mediaToken, chat_type, sender_alias, msg_uuid, reply_uuid} = await helpers.parseReceiveParams(payload)
if(!owner || !sender || !chat) {
return console.log('=> no group chat!')
}
Expand All @@ -149,7 +152,8 @@ const receivePayment = async (payload) => {
if(mediaToken) msg.mediaToken = mediaToken
if(chat_type===constants.chat_types.tribe) {
msg.senderAlias = sender_alias
}
}
if(reply_uuid) msg.replyUuid = reply_uuid

const message = await models.Message.create(msg)

Expand Down
21 changes: 6 additions & 15 deletions api/utils/socket.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import * as WebSocket from 'ws'

// let connections = new Map()
// let connectionCounter = 0

let lastConn: any
let srvr: any

const connect = (server) => {
server = new WebSocket.Server({ server })

srvr = new WebSocket.Server({ server, clientTracking:true })
console.log('=> [socket] connected to server')

server.on('connection', socket => {
srvr.on('connection', socket => {
console.log('=> [socket] connection received')
// var id = connectionCounter++;
// connections.set(id, socket)
lastConn = socket
})

}

const send = (body) => {
// connections.forEach((socket, index) => {
// socket.send(body)
// })
if(lastConn) lastConn.send(body)
srvr.clients.forEach(c=>{
if(c) c.send(body)
})
}

const sendJson = (object) => {
Expand Down
16 changes: 12 additions & 4 deletions dist/api/controllers/media.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/api/controllers/media.js.map

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions dist/api/controllers/payment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b93b37d

Please sign in to comment.