1 Star 0 Fork 11

星尘 / ws2s

forked from 03 / ws2s 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ws2s--bring socket to browser-side js

ws2s(websocket to socket) is a websocket server that handle socket(tcp)s.

the simplest workflow might be like this:

first, a websocket client(we called "ws") ask ws2s_server to create a socket(we called "s") for it.
then, "ws" ask ws2s_server to use "s" to send data.
when "s" received data, ws2s_server will notify "ws" with the received data.

with this workflow, javaScript running on a browser got the ability to use socket.

client case

a ws2s server at wss://feling.io/ws2s-server/ is ready for test case.
an online redis gui client powered by ws2s is provided at fredis.

use origin javaScript

var ws = new WebSocket("wss://feling.io/ws2s-server/")
ws.onmessage = (event) => {
    console.log("onmessage: ", event.data)
}
ws.onopen = () => {
    console.log("onopen")
    ws.send(JSON.stringify(
        {
            command: "connect",
            host: "feling.io",
            port: 80
        }
    ))
    ws.send(JSON.stringify(
        {
            command: "send",
            data: "GET / HTTP/1.1\r\nHost: feling.io\r\nConnection: close\r\n\r\n"
        }
    ))
}
ws.onclose = () => {
    console.log("onclose")
}

use ws2s.js

var socket = new WS2S("wss://feling.io/ws2s-server/").newSocket()

$('#connect-button').bind("click", () => {
    socket.connect("feling.io", 80)
})

$('#send-button').bind("click",  () => {
    socket.send("GET / HTTP/1.1\r\nHost: feling.io\r\nConnection: close\r\n\r\n")
})

socket.onRecv = (data) => {
    console.log('onRecv', data)
}

install

ws2s works on py2、py3、Linux、OSX. I tried to support Windows, but there is too much adaptation to be handled for Windows.

It is recommended to install from github:

pip install git+https://github.com/playlay/ws2s --upgrade

you can also install ws2s from pypi:

pip install ws2s-python --upgrade

after installed ws2s:
ws2sd command can be used in shell,
~/.ws2s/ directory will be created when you exec ws2sd

config

config file is store at ~/.ws2s/config.json.

protocol

request

all kinds of requests are listed below:

{
    "command": "connect",
    "host":"127.0.0.1",
    "port":80
}
{
    "command": "send",
    "data":"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n"
}
{
    "command": "close"
}

you can find out that:

  1. message(we called request) send to ws2s_sever is a json format string.
  2. a "command" field is required

response

message(we called response) received from ws2s_sever, is a json format string too:

{
    "success": true,
    "code": -1,
    "message": "recv data",
    "data": []
}
As the example above:    
- "message" field is for human.   
- "success" field can be ignored.     

when "code" field = -1, "data" field is presented.     
    that means ws2s_server received data from peer. "data" is a int array, each element represents a byte.     

when "code" field = 0.      
    usually means ws2s_server successfully completed the most recent command    

when "code" field > 0.      
    means something is not normal:      
    when "code" = 1:    
        unknown exception, you can submit an issue to ws2s    

    when "code" = 2:    
        ConnectionRefusedError raisesd when ws2s_server try to   
        connect host:port you specified    

    when "code" = 3:    
        IllegalSocketState, just reSend an "connect" request like:    
        {"command":"connect","host":"127.0.0.1","port":80}    

    when "code" = 4:    
        usually means you want ws2s_server to connect 127.0.0.1,    
        but ws2s_server refused to do that     
    
    when "code" = 5:    
        socket connection closed by socket server you connected to
# The MIT License (MIT) # Copyright (c) 2018 chenyan # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE.

简介

ws2s(websocket to socket)让运行在浏览器上的js,能使用socket来通信。 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/xingchensoft/ws2s.git
git@gitee.com:xingchensoft/ws2s.git
xingchensoft
ws2s
ws2s
master

搜索帮助