40行代码实现一个网页聊天室
发布于 6 年前 作者 xcstream 2687 次浏览 来自 分享

https://xcstream.github.io/achat/ 打开2个窗口就可以发消息了 代码非常简单易懂

2 回复

然后呢 源码呢。。

    var client = new Paho.MQTT.Client("home.appxc.com", Number(10100), ""+Math.random())
    client.onMessageArrived = function(message) { window.vm.messages.push(JSON.parse(message.payloadString).content)}
    client.connect({useSSL:true, onSuccess:function(){ client.subscribe("public")}})
    window.vm=new Vue({
        el:'body>div',
        data:{messages:[], tosend:''},
        methods:{
            send:function(){
                if(this.tosend == "") return
                var payload = ({content:this.tosend})
                var message = new Paho.MQTT.Message(JSON.stringify(payload))
                message.destinationName = "public"
                client.send(message)
                this.tosend = ""
            }
        }
    })
	````
回到顶部