CNode

40行代码实现一个网页聊天室

分享
Xxcstream__legacy_582491551120be9438b02a99发布于9 年前最后回复9 年前2 回复3314 浏览0 收藏

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

查看回复

回复 (2)

I
ipengyo#1·9 年前

然后呢 源码呢。。

I
im-here#2·9 年前
    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 = ""
            }
        }
    })
	````
参与回复
登录后即可参与回复。登录