Hi there,
currently i am trying to create a plugin, which provides a socketserver and sends to all connected clients some text if you hit a key-combination.
Here is what i got for now:
[code]import SocketServer, thread, sublime, sublime_plugin, pprint
HOST = “localhost”
PORT = 12321
class EchoRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
while True:
pprint.pprint(repr(self.request))
self.data = self.request.recv(1024)
if not self.data:
break
self.data = self.data.strip()
class socketapplication():
def run(self):
self.mythread = thread.start_new_thread(self.start, ())
def start(self):
self.server = SocketServer.TCPServer((HOST, PORT), EchoRequestHandler)
self.server.serve_forever()
if ‘app’ in globals():
app.server.shutdown()
app = socketapplication()
app.run()
class sendsomethingCommand(sublime_plugin.TextCommand):
def run(self, edit):
app.server.socket.sendall(“this-is-a-test”)[/code]
As you can see, i am getting an instance of socketapplication and save it to the global variable app.
After that, i am calling the run method which creates a thread in which the socket gets created.
The Server works fine, i am able to connect and send data within the handle method of the EchoRequestHandler.
But within the sendsomethingCommand-run method, i want to tell the socket to send data to all of the clients.
Currently “app.server.socket.sendall(“this-is-a-test”)” throws an error like this:
[quote]Traceback (most recent call last):
File “.\sublime_plugin.py”, line 362, in run_
File “.\socketapplication.py”, line 40, in run
File “”, line 1, in sendall
socket.error: [/quote]
I hope you guys can help me sending data within the sendsomethingCommand class to all clients.
Kindly regards,
Philipp