Skip to content

Instantly share code, notes, and snippets.

@frzleaf
Last active July 7, 2019 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frzleaf/b64652bd7c24fcf167f620a69878047f to your computer and use it in GitHub Desktop.
Save frzleaf/b64652bd7c24fcf167f620a69878047f to your computer and use it in GitHub Desktop.
Auto cvnl.app
// I'm not a chatvn.me member but like the way they do
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Start
let imported = document.createElement('script')
imported.src = 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.min.js'
document.head.appendChild(imported)
const sendType = {
new_chat: "c1",
chat_message: "c2",
online_user: "c3",
typing: "c4",
close_chat: "c5",
like_conversation: "c6",
block: "c7",
report: "c8",
init: "c9",
forceClose: "c10"
}
const messages = ["Chào bạn, qua http://chatvn.me chat với mình nhé"]
let socket = null
setTimeout(_ => {
socket = io("https://gth-talk-stranger.herokuapp.com", {
forceNew: false,
path: "/socket.io",
port: "443",
query: "token=" + localStorage.token,
secure: true,
})
socket.on('connect', _ => {
console.log('Started')
newChat()
setTimeout(_ => {
setupRules()
}, 500)
})
}, 1000)
let lastMessage = {
content: null,
time: 0
}
let lastGreetingTime = 0
let stopEndchat = false
let lastGreetingFuncId = 0
function setupRules() {
socket.on(sendType.chat_message, data => {
lastMessage = {
content: data.content,
time: Date.now()
}
})
socket.on(sendType.typing, _ => {
lastMessage = {
content: 'User typing',
time: Date.now()
}
})
socket.on(sendType.new_chat, data => {
if (data.gender == 'female') { // select gender to chat with
stopEndchat = true
clearTimeout(lastGreetingFuncId)
setTimeout(_ => {
lastGreetingTime = Date.now()
sendMessage(getRandomGreeting())
lastGreetingFuncId = setTimeout(checkGreetingReply, 10000)
stopEndchat = false
}, 1500)
} else {
setTimeout(closeChat, 500)
}
})
socket.on(sendType.close_chat, _ => { newChat() })
}
function checkGreetingReply() {
if (lastGreetingTime - lastMessage.time > 10000) {
closeChat()
}
}
function sendMessage(message) {
socket.emit('message', {
type: sendType.chat_message,
data: {
content: message,
uuid: uuidv4()
}
})
}
function closeChat() {
if (stopEndchat) {
return
}
socket.emit('message', {
type: sendType.close_chat
})
setTimeout(_ => {
newChat()
}, 1000)
}
function newChat() {
socket.emit('message', {
type: sendType.new_chat
})
}
function getRandomGreeting() {
return messages[Math.floor(Math.random() * messages.length)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment