GithubHelp home page GithubHelp logo

socket.io.xcx's Introduction

微信小程序版 socket.io和engine.io

可以使用在微信小程序中的socket.io和engine.io

微信小程序的websocket做了一次封装,导致socket.io或者engine.io客户端包,无法直接使用 ,该项目在engine.io-client项目基础上,实现wxwebsocket的传输层,使得在小程序中能使用socket.io的所有功能。

注意:编译出来的包只能在微信小程序当中使用

Build Status

安装

$ git clone https://github.com/wujjpp/socket.io.xcx.git
$ cd socket.io.xcx
$ npm install

运行测试服务器&测试微信小程序

  1. 运行测试服务器
$ npm run serve
  1. 使用 "微信web开发者工具" 打开examples/app项目,运行样例小程序

注意:运行测试程序,请开启"开发环境不校验请求域名及TLS版本"

生产环境中使用

复制dist/socket.io.xcx.min.js到您的小程序源码,直接使用,具体可参照examples/app

代码片段

该实例通过emit message向服务端发送 hello 字串,服务端收到后 emit message 到客户端

client -> server : hello

server -> client : 'hello world '

小程序端

~/examples/app/pages/index/index.js

// 导入socket.io小程序版包
import io from '../../socket.io.xcx.min'

// 使用IO创建socket实例,本实例使用本地socket.io服务器, 请根据根据实际情况修改IP
let socket = io('ws://127.0.0.1:3000')

function _setup() {
    var self = this;

    socket.on('connect', function () {
        console.log('连上了');
    });

    socket.on('message', function (data) {
        self.data.messages.push(data.message);
        self.setData({
            messages: self.data.messages
        })
    });

    socket.on('disconnect', function () {
        console.log('you have been disconnected');
    });
}

Page({
    data: {
        messages: []
    },
    onLoad: function () {
        _setup.apply(this);
    },
    send: function () {
        socket.emit('message', 'hello');
    }
})

服务器端

~/examples/server/app.js

// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;

server.listen(port, function () {
    console.log('Server listening at port %d', port);
});

io.on('connection', function (socket) {

    // when the client emits 'message', this listens and executes
    socket.on('message', function (data) {

        console.log('message from client: ', data);

        io.emit('message', {
            message: data + ' world ' + new Date()
        });
    });

    // when the user disconnects.. perform this
    socket.on('disconnect', function () {
        // echo globally that this client has left
        socket.broadcast.emit('user left', {
            message: 'user left'
        });

    });
});

Made with ♥ by Wu Jian Ping

socket.io.xcx's People

Contributors

wujjpp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

socket.io.xcx's Issues

在小游戏项目中 import 报错

gameThirdScriptError
Cannot use 'in' operator to search for 'WebkitAppearance' in undefined
TypeError: Cannot use 'in' operator to search for 'WebkitAppearance' in undefined
    at Function.useColors (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:993:85)
    at createDebug (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:1045:107)
    at Object.<anonymous> (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:565:72)
    at Object.r.12.debug (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:588:10)
    at s (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:27:49)
    at http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:28:37
    at Object.r.8.../../socket.io-parser (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:329:17)
    at s (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:27:49)
    at e (http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:32:7)
    at http://127.0.0.1:55020/game/js/libs/socket.io.xcx.min.js:34:4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.