Skip to content

Commit

Permalink
Rebase and minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
danixeee committed Jun 15, 2021
1 parent 2190b5e commit 6068321
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
13 changes: 13 additions & 0 deletions examples/json-extension/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@
"env": {
"PYTHONPATH": "${workspaceFolder}"
}
},
{
"name": "Launch Server [WebSockets]",
"type": "python",
"request": "launch",
"module": "server",
"args": ["--ws"],
"justMyCode": false,
"python": "${command:python.interpreterPath}",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${workspaceFolder}"
}
}
],
"compounds": [
Expand Down
8 changes: 7 additions & 1 deletion examples/json-extension/server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def add_arguments(parser):

parser.add_argument(
"--tcp", action="store_true",
help="Use TCP server instead of stdio"
help="Use TCP server"
)
parser.add_argument(
"--ws", action="store_true",
help="Use WebSocket server"
)
parser.add_argument(
"--host", default="127.0.0.1",
Expand All @@ -46,6 +50,8 @@ def main():

if args.tcp:
json_server.start_tcp(args.host, args.port)
elif args.ws:
json_server.start_ws(args.host, args.port)
else:
json_server.start_io()

Expand Down
5 changes: 2 additions & 3 deletions pygls/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def __init__(self, server):
self.transport = None
self._message_buf = []

self._send_only_data = False
self._send_only_body = False

def __call__(self):
return self
Expand Down Expand Up @@ -375,8 +375,7 @@ def _send_data(self, data):
logger.info('Sending data: %s', body)

body = body.encode(self.CHARSET)

if not self._send_only_data:
if not self._send_only_body:
header = (
f'Content-Length: {len(body)}\r\n'
f'Content-Type: {self.CONTENT_TYPE}; charset={self.CHARSET}\r\n\r\n'
Expand Down
18 changes: 10 additions & 8 deletions pygls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from threading import Event
from typing import Any, Callable, List, Optional, TypeVar

import websockets

from pygls import IS_WIN
from pygls.lsp.types import (ApplyWorkspaceEditResponse, ClientCapabilities, ConfigCallbackType,
ConfigurationParams, Diagnostic, MessageType, RegistrationParams,
Expand Down Expand Up @@ -106,7 +104,7 @@ class WebSocketTransportAdapter:
Write method sends data via the WebSocket interface.
"""

def __init__(self, ws: websockets.WebSocketServerProtocol, loop):
def __init__(self, ws, loop):
self._ws = ws
self._loop = loop

Expand Down Expand Up @@ -231,17 +229,21 @@ def start_tcp(self, host, port):
finally:
self.shutdown()

def start_websocket(self, host, port, path='/'):
def start_ws(self, host, port):
"""Starts WebSocket server."""
try:
import websockets
except ImportError:
logger.error('Run `pip install pygls[ws]` to install `websockets`.')
sys.exit(1)

logger.info('Starting WebSocket server on {}:{}'.format(host, port))

# TODO: Handle the path
self._stop_event = Event()
self.lsp._send_only_data = True # Don't send headers within the payload
self.lsp._send_only_body = True # Don't send headers within the payload

async def connection_made(websocket, cur_path):
async def connection_made(websocket, _):
"""Handle new connection wrapped in the WebSocket."""
# TODO: How to handle the path argument?
self.lsp.transport = WebSocketTransportAdapter(websocket, self.loop)
async for message in websocket:
self.lsp._procedure_handler(
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ exclude =
tests.*

[options.extras_require]
ws =
websockets==9.*
dev =
bandit==1.6.0
flake8==3.7.7
Expand Down

0 comments on commit 6068321

Please sign in to comment.